Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python pandas dataframes
import pandas as pd
import numpy as np
from collections import defaultdict
df1 = pd.DataFrame({"col1":[0,1,2,3],"col2":[4,5,6,7],"col3":[8,9,10,11]},index=["idx1","idx2","idx3","idx4"])
print("df1")
print(df1,end="\n\n")
df2 = pd.DataFrame([[0,4,8],[1,5,9],[2,6,10],[3,7,11]],index=["idx1","idx2","idx3","idx4"],columns=["col1","col2","col3"])
print("df2")
print(df2,end="\n\n")
print("df1 == df2")
print(df1==df2,end="\n\n")
df3 = pd.DataFrame(np.arange(12).reshape((4,3)),
index=[["idx1-1","idx1-2","idx1-2","idx1-2"],["idx2-1","idx2-2","idx2-3","idx2-3"],["idx3-1","idx3-2","idx3-3","idx3-4"]],
columns=["col1","col2","col3"])
print("df3(MultiIndex) #1")
print(df3,end="\n\n")
df3["col4"] = 100
print("df3(MultiIndex) #2: Append column")
print(df3,end="\n\n")
print("df3(MultiIndex) #3: Access to scalar value")
print(df3["col2"]["idx1-2","idx2-3","idx3-4"],end="\n\n")
print("df3(MultiIndex) #4: Access to scalar by ix property")
print(df3.ix[("idx1-2","idx2-3","idx3-4"),"col3"],end="\n\n")
print("df3(MultiIndex) #5: Access to record by ix with condtions")
print(df3.ix[1:3,[True,False,True]],end="\n\n")
print("df3(MultiIndex) #6: Access to record by ix with conds specified by isin method")
print(df3.ix[df3.index.isin([("idx1-1","idx2-1","idx3-1"),("idx1-2","idx2-3","idx3-3")]),df3.columns.isin(["col3","col2"])],end="\n\n")
print("df3(MultiIndex) #7: Pivot table with fillna method")
print(pd.pivot_table(df3, values='col1',index=['col2'],columns=['col3']).fillna(-1),end="\n\n")
print("df3(MultiInex) #8: Stack method")
print(df3.stack(),end="\n\n")
print("df3(MultiIndex) #9: Stack and unstack")
print(df3.stack().unstack(),end="\n\n")
print("df3(MultiIndex) #10: Unstack method")
print(df3.unstack(),end="\n\n")
print("df3(MultiIndex) #11: Nested unstacks")
print(df3.unstack().unstack(),end="\n\n")
print("df3(MultiIndex) #12: Swaplevel method")
print(df3.swaplevel(0,2),end="\n\n")
i=0
df4 = defaultdict(lambda:defaultdict(lambda:defaultdict(int)))
for k1 in ["a1","a2","a3"]:
for k2 in ["b1","b2","b3"]:
for k3 in ["c1","c2","c3"]:
df4[k1][k2][k3] = i
i+=1
print("df4(Nested dict)")
print(pd.DataFrame(df4),end="\n\n")
i=0
df5 = defaultdict(tuple)
for k1 in ["a1","a2","a3"]:
for k2 in ["b1","b2","b3"]:
for k3 in ["c1","c2","c3"]:
df5[(k1,k2,k3)] = [i]
i+=1
print("df5(Dict with tuple key) #1")
print(pd.DataFrame(df5,index=[""]),end="\n\n")
print("df5(Dict with tuple key) #2: Stack method")
print(pd.DataFrame(df5,index=[""]).stack(),end="\n\n")
print("df5(Dict with tuple key) #3: Nested stacks")
print(pd.DataFrame(df5,index=[""]).stack().stack().stack(),end="\n\n")
print("df5(Dict with tuple key) #4: Nested stacks and swaplevel")
print(pd.DataFrame(df5,index=[""]).stack().stack().stack().swaplevel(1,3),end="\n\n")
print("df5(Dict with tuple key) #5: Nested stacks, swaplevel and unstack")
print(pd.DataFrame(df5,index=[""]).stack().stack().stack().swaplevel(1,3).unstack(),end="\n\n")
print("df5(Dict with tuple key) #6: Swaplevel with axis=1")
print(pd.DataFrame(df5,index=[""]).swaplevel(0,2,axis=1),end="\n\n")
i=0
df6 = defaultdict(tuple)
for k1 in ["a1","a2","a3"]:
for k2 in ["b1","b2","b3"]:
for k3 in ["c1","c2","c3"]:
df6[(k1,k2,k3)] = [i]
i+=1
print("df6(Dict with tuple key and list values)")
print(pd.DataFrame(df6),end="\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.