Contents
Description
Pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.
Snippets
Standard import
Read CSV file
Variable |
Meaning |
sep |
separator character, can be an array |
header |
row number that contains column titles. remove if none |
dtype |
force any column to be interpreted as specific datatype |
parse_dates |
parse any column as datetime |
encoding |
file encoding e.g. utf-8 or ansi |
Renaming Columns
Drop Column
1 df.drop('Column A', axis=1, inplace=True)
Do Y for each value in column X
1 df.loc[:, ['X']] = df.loc[:, ['X']].apply(lambda x: x.Y)
Concat DataFrames
reset_index() is called to get rid of duplicate indices
Filter by regex on column A
1 df = df[df['A']str.match('regex')]
Merge DataFrames
Sort DataFrame
1 df = df.sort_values('Column A', ascending = False)
Pivot Table
1 pvt_df = pd.pivot_table(df,
2 values=['Column B', 'Column C'],
3 index='Column A',
4 aggfunc={'Column B': [np.sum, np.average],
5 'Column C' : lambda x: len(x)})
6 pvt_df .reset_index(inplace=True) # Make Column A a column again, instead of index
7 pvt_df .columns = [' '.join(col).strip() for col in
8 pvt_df.columns.values] # Flatten multi-index columns
9 pvt_df .rename(columns={ # Rename known numbers
10 'Column B average':'average_B',
11 'Column B sum':'sum_B',
12 'Column C <lambda>':'count_C'
13 }, inplace=True)