DataFrame From Python

Creating Pandas DataFrames from Python Lists and Dictionaries

Dictionary

List

s al es = [ { ' ac c ount ' : ' J ones LLC' , ' J an' : 150, ' Feb' : 200, ' Mar ' : 140} , { ' ac c ount ' : ' Al pha Co' , ' J an' : 200, ' Feb' : 210, ' Mar ' : 215} , { ' ac c ount ' : ' Bl ue I nc ' , ' J an' : 50, ' Feb' : 90, ' Mar ' : 95 } ]

df = pd. Dat aFr ame( s al es )

s al es = [ ( ' J ones LLC' , 150, 200, 50) , ( ' Al pha Co' , 200, 210, 90) , ( ' Bl ue I nc ' , 140, 215, 95) ]

l abel s = [ ' ac c ount ' , ' J an' , ' Feb' , ' Mar ' ] df = pd. Dat aFr ame. f r om_r ec or ds ( s al es , c ol umns =l abel s )

Row Oriented

Column Oriented

def aul t

account

Jan

Feb

Mar

0

Jones LLC 150

200

140

1

Alpha Co

200

210

215

2

Blue Inc

50

90

95

f r om_r ec or ds

s al es = { ' ac c ount ' : [ ' J ones LLC' , ' Al pha Co' , ' Jan' : [ 150, 200, 50] , ' Feb' : [ 200, 210, 90] , ' Mar ' : [ 140, 215, 95] }

df = pd. Dat aFr ame. f r om_di c t ( s al es )

' Bl ue I nc ' ] ,

f r om_di c t

When using a dictionary, column order is not preserved. Explicitly order them:

df = df [ [ ' ac c ount ' , ' J an' , ' Feb' , ' Mar ' ] ]

s al es = [ ( ' ac c ount ' , [ ' J ones LLC' , ' Al pha Co' , ( ' Jan' , [ 150, 200, 50] ) , ( ' Feb' , [ 200, 210, 90] ) , ( ' Mar ' , [ 140, 215, 95] ) ]

df = pd. Dat aFr ame. f r om_i t ems ( s al es )

' Bl ue I nc ' ] ) ,

f r om_i t ems

Practical Business Python -

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download