User Tools

Site Tools


pandas_helper

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pandas_helper [2015/12/16 12:51] (current)
vincenzo created
Line 1: Line 1:
 +DataFrame: Made of Dictionaries
 +<code python>
 +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
 +</​code>​
 +
 +Selecting Values in DataFrame
 +<code python>
 +exampledf.loc[exampledf.column2 == "​ab","​column2"​] = "​dd"​
 +
 +exampledf["​column1"​]
 +
 +exampledf["​column2"​][2]
 +</​code>​
 +
 +Filtering DataFrames
 +
 +<code python>
 +exDFfilter1 = exampledf[exampledf.column2.isin(['​aa',​ '​ab'​])]
 +exDFindex2 = exampledf.iloc[2]
 +
 +exDFfilter2 = exampledf[exampledf['​column2'​].str.contains('​a'​)]
 +
 +</​code>​
 +
 +Add or Remove Columns
 +<code python>
 +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]
 +
 +</​code>​
 +
 +Add or Remove Rows
 +
 +<code python>
 +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]])
 +</​code>​
 +Combining Data Frames
 +<code python>
 +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
 +</​code>​
 +
 +
  
pandas_helper.txt ยท Last modified: 2015/12/16 12:51 by vincenzo