Skip to content

Instantly share code, notes, and snippets.

@Kukanani
Created April 27, 2018 22:00
Show Gist options
  • Save Kukanani/ebd642c484eedb71753b33051ca06b3b to your computer and use it in GitHub Desktop.
Save Kukanani/ebd642c484eedb71753b33051ca06b3b to your computer and use it in GitHub Desktop.
Pandas Cheat Sheet
import pandas as pd
import numpy as np
####################################
# BASICS
####################################
# load from CSV
# see https://chrisalbon.com/python/data_wrangling/pandas_dataframe_importing_csv/
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv')
# Column Lookup
# The two following are equivalent.
df['foo']
df.foo
####################################
# COLUMN WIZARDRY
####################################
# Delete a column
del df['worthless']
# Create new column based on values of other columns
df['c'] = df.apply(lambda row: row.a + row.b, axis=1)
# Create column with arbitrary function
def foo(a, b):
return a + b
df['c'] = df.apply(lambda row: foo(row['a'], row.b), axis=1)
####################################
# Stats
df['weight'].mean()
# Categories in categorical data
df['category_field'].cat.categories.tolist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment