Definition of Dataframe
1data = {2 'Color': ['Blue','Green','Yellow','Red',' White'],3 'Object': [' Ball','Pen','Pecil','Paper','Mug'],4 ' Price': [1.2, 1, 2.3, 5, 6]5 }6FRAME0 =PD. DataFrame (data)7 Print(FRAME0)8Frame1 = PD. DataFrame (data, columns=['Object',' Price'])9 Print(frame1)Tenframe2 = PD. DataFrame (data, index=['Zhang San','Reese','Harry','Chen Jiu','Xiao Ming']) One Print(frame2) A out[1]: - Color Object Price -0 Blue Ball 1.2 the1 Green Pen 1.0 -2 Yellow Pecil 2.3 -3 Red Paper 5.0 -4 White Mug 6.0 + Object Price -0 Ball 1.2 +1 Pen 1.0 A2 Pecil 2.3 at3 Paper 5.0 -4 Mug 6.0 - Color Object Price -Zhang San Blue ball 1.2 -Reese Green Pen 1.0 -Harry Yellow Pecil 2.3 inChen Jiu Red Paper 5.0 -Xiao Ming White Mug 6.0
Use the index parameter to set the index information
Select an element
1 Print(frame1.columns)2 Print(Frame2.index)3 Print(frame2[' Price'])4 Print(Frame2.price)5 out[2]: 6Index (['Object',' Price'], dtype='Object')7Index (['Zhang San','Reese','Harry','Chen Jiu','Xiao Ming'], dtype='Object')8Zhang San 1.29Reese 1.0TenHarry 2.3 OneChen Jiu 5.0 AXiao Ming 6.0 - Name:price, Dtype:float64 -Zhang San 1.2 theReese 1.0 -Harry 2.3 -Chen Jiu 5.0 -Xiao Ming 6.0 +Name:price, Dtype:float64
In general, we often need to value by column, then Dataframe provides loc and Iloc for everyone to choose from, but the difference is between the two.
1 Print(frame2)2 Print(frame2.loc['Harry'])#Loc can use the index of the string type, whereas the Iloc can only be of type int3 Print(frame0.iloc[2])4 out[2]: 5 Color Object Price6Zhang San Blue ball 1.27Reese Green Pen 1.08Harry Yellow Pecil 2.39Chen Jiu Red Paper 5.0TenXiao Ming White Mug 6.0 One Color Yellow A Object Pecil -Price 2.3 - Name: Harry, Dtype:object the Color Yellow - Object Pecil -Price 2.3 -Name:2, Dtype:object
General value operation
1 Print(Frame2[2:3])#Fetch Rows2 Print(frame0['Object'])#Fetching Columns3 Print(frame0['Object'][1:3])#take the element of the column4 Print(Frame0.iloc[0:4, 1:3])#take a piece of the element ********************************************************************5 out[3]: 6 Color Object Price7Harry Yellow Pecil 2.38 0 Ball91PenTen2Pecil One3Paper A4Mug - Name:object, Dtype:object -1Pen the2Pecil - Name:object, Dtype:object - Object Price -0 Ball 1.2 +1 Pen 1.0 -2 Pecil 2.3 +3 Paper 5.0
Python Data Analysis Library pandas------DataFrame