User Tools

Site Tools


pandas_helper

DataFrame: Made of Dictionaries

import pandas as pd
 
example = {"column1":["a","b","c"],"column2":["aa","ab","cc"],"column3":[1,2,3],"column4":[4,5,6]}
exampledf = pd.DataFrame(example)
exampledf

Selecting Values in DataFrame

exampledf.loc[exampledf.column2 == "ab","column2"] = "dd"
 
exampledf["column1"]
 
exampledf["column2"][2]

Filtering DataFrames

exDFfilter1 = exampledf[exampledf.column2.isin(['aa', 'ab'])]
exDFindex2 = exampledf.iloc[2]
 
exDFfilter2 = exampledf[exampledf['column2'].str.contains('a')]

Add or Remove Columns

exampledf["column5"] = [7,8,9]
exampledf["column6"] = 10
exDF6col = exampledf
exDF6col
 
exDF3col = exampledf.drop('column4', 1)
exDF3col
 
exDF2col = exampledf.drop(["column1","column2"],1)
exDF2col
 
exampledf.loc[2]
exampledf.ix[0:1]
 
exampledf[2:3]

Add or Remove Rows

exampledf.loc[5] = "empty"
exampledf
 
exampledf.loc[5] = ["d","dd",4,7,10,10]
exampledf
 
exampledf.loc[2,"column2"] = "dddd"
exampledf
 
exampledf.drop(exampledf.index[[1,3]])

Combining Data Frames

exampledf2 = pd.DataFrame({"column2":["ab","dddd","dd","aa"],"column8":["10","15","20","25"],"column9":["some","thing","else","here"]})
# Merge (similar to VLookup)
exampledf3 = exampledf.merge(exampledf2, on="column2", how="left")
exampledf3
 
# append two similiar DataFrames vertically
exampledf4 = pd.DataFrame({"column1":["ab","dddd","dd","aa"],"column2":["10","15","20","25"],"column3":["some","thing","else","here"]})
exampledf5 = pd.DataFrame({"column1":["abc","ddddd","ddd","aaa"],"column2":["100","150","200","250"],"column3":["somemore","thingies","else","hereby"]})
exampledf6 = exampledf4.append(exampledf5,ignore_index=True)
exampledf6
 
# append two different DataFrames horizontally
exampledf7 = pd.DataFrame({"column1":["ab","dddd","dd","aa"],"column2":[10,15,20,25],"column3":["some","thing","else","here"]})
exampledf8 = pd.DataFrame({"column4":["abc","ddddd","ddd","aaa"],"column16":[100,150,200,250],"column22":["somemore","thingies","else","hereby"]})
exampledf9 = exampledf7.join(exampledf8)
exampledf9
pandas_helper.txt · Last modified: 2015/12/16 12:51 by vincenzo