-
Notifications
You must be signed in to change notification settings - Fork 0
Pandas cheat sheet
Zakir Syed edited this page Feb 1, 2019
·
20 revisions
df.columns = df.columns.str.replace(r'\s+', '_') #This replaces ' ' with '_' in columns
df.columns is of type index dence can be converted to string and cleaned up in a chained fashion
df.columns = df.columns.str.replace('.', '_').str.replace('(','').replace(')','')
for row in df.itertuples():
print(f"{row.Col_1} : {row.Col_2}")
This Stack Overflow Link has in-depth analysis with performance plotted in a great amount of detail. Here are a few convenient ways:-
df[df['col'] == val] # Slows down with df length
df[df['col'].values == val] # np version scales up very well
df.query('col == val') # Scales up well
df['Name'] = df['Name'].str[:5] #Strip name to 5 char s only
df = df.assign(PassengerId = 420) #Update all Passenger's ID to 420
df['Survived'] = df['Survived'].apply(lambda x: 'Fortunate' if x==1 else 'Unfortunate')