Cheatography
https://cheatography.com
Frequently Used Python Commands
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Creating a Dataframe (copy)
Pandas DataFrame
df = pd.merge(left=left_df, right=right_df, on='FIELD', validate='m:m')
|
Select Rows based on column value df.loc[df['column_name'] == some_value] df[(df.field1 cond1) & (df.field2 cond2)]
|
Sort (by specified Field) df.sort_values('FIELD')
|
Fill a column with value
|
Reset Index df.reset_index(inplace=True)
|
Extract [month, date, hour, etc] from datetime column df['NEW_FIELD'] = df['dt_FIELD'].dt.<value>
value=month, date, hour
|
Set Index df.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)
|
|
|
pandas Functions
len(df) |
Number of rows in DataFrame |
df.dtypes |
Data type of each column |
df.columns |
DataFrame column names |
list of str |
df.count() |
Number of values in each column |
df.sum() |
Sum of values in each column |
df.min() |
Minimum value in each column |
df.max() |
Maximum value in each column |
df.mean() |
Mean value in each column |
Replace df with df["Column Name"] or an equivalent variable to use these functions for a single column or set of selected values.
Reading and writing data
Excel df = pd.read_excel('file.xlsx', sheet_name= 'sheet name) [...] df.to_excel('file.xlsx', sheet_name='sheet name' [...]
|
Excel multi sheets with pd.ExcelWriter('path_to_file.xlsx') as writer: df1.to_excel(writer, sheet_name='Sheet1') df2.to_excel(writer, sheet_name='Sheet2')
|
pickle df = pd.read_pickle('file.pickle') [...] df.to_pickle('file.pickle') [...]
|
csv df = pd.read_csv('file.csv') [...]
|
|
|
Class Property Ddecorators
@property
def val(self):
return self._val
@val.setter
def temperature(self, value):
self._val = value
|
|