-
Notifications
You must be signed in to change notification settings - Fork 0
Pandas cheat sheet
Zakir Syed edited this page Feb 14, 2019
·
20 revisions
for row in df.itertuples():
print(f"{row.Col_1} : {row.Col_2}")
for column in df:
plt.plot(column)
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.loc['row_1':'row_2', 'col_1':'col_2'] #Label based
df.iloc[10:20, 2:6] # 0-based indexes; excludes last item
df.drop(['col_name'], axis=1, inplace=True)
df.insert(loc=3, column='col_name', value=new_col)
df.columns = df.columns.str.replace(r'\s+', '_') #This replaces ' ' with '_' in columns
df.columns is of type index hence can be converted to string and cleaned up in a chained fashion
df.columns = df.columns.str.replace('.', '_').str.replace('(','').replace(')','')
embarked_dummies = pd.get_dummies(df['Embarked'], prefix='Embarked@')
embarked_dummies.head()
df = pd.merge(df, embarked_dummies, left_index=True, right_index=True).drop(['Embarked'], axis=1)
df.head()