缩写解释 & 库的导入
df --- 任意的pandas DataFrame(数据框)对象
s --- 任意的pandas Series(数组)对象
pandas和numpy是用Python做数据分析最基础且最核心的库
In [1]:
import pandas as pd # 导入pandas库并简写为pd
import numpy as np # 导入numpy库并简写为np
数据的导入
pd.read_csv(filename)# 导入csv格式文件中的数据
pd.read_table(filename)# 导入有分隔符的文本 (如TSV) 中的数据
pd.read_excel(filename)# 导入Excel格式文件中的数据
pd.read_sql(query,connection_object)# 导入SQL数据表/数据库中的数据
pd.read_json(json_string)# 导入JSON格式的字符,URL地址或者文件中的数据
pd.read_html(url)# 导入经过解析的URL地址中包含的数据框 (DataFrame) 数据
pd.read_clipboard()# 导入系统粘贴板里面的数据
pd.DataFrame(dict)# 导入Python字典 (dict) 里面的数据,其中key是数据框的表头,value是数据框的内容。
数据的导出
df.to_csv(filename)# 将数据框 (DataFrame)中的数据导入csv格式的文件中
df.to_excel(filename)# 将数据框 (DataFrame)中的数据导入Excel格式的文件中df.to_sql(table_name,connection_object)# 将数据框 (DataFrame)中的数据导入SQL数据表/数据库中df.to_json(filename)# 将数据框 (DataFrame)中的数据导入JSON格式的文件中
创建测试对象
pd.DataFrame(np.random.rand(10,5))# 创建一个5列10行的由随机浮点数组成的数据框 DataFrame
In [2]:
pd.DataFrame(np.random.rand(10,5))
Out[2]:
01234
00.6477360.3726280.2558640.8535420.613267
10.0643640.1563400.5750210.5619110.479901
20.0364730.8768190.2553250.3932400.543039
30.3574890.0065780.0939660.5312940.029009
40.5505820.5046000.2735460.0116930.052523
50.7215630.1706890.7021630.4478830.905983
60.8397260.9359970.3431330.3569570.377116
70.9318940.0266840.7191480.9114250.676187
80.1156190.1148940.1306960.3215980.170082
90.1946490.5261410.9654420.2754330.880765
pd.Series(my_list)# 从一个可迭代的对象 my_list 中创建一个数据组
In [3]:
my_list=['Kesci',100,'欢迎来到科赛网']
pd.Series(my_list)
Out[3]:
0 Kesci
1 100
2 欢迎来到科赛网
dtype: object
df.index=pd.date_range('2017/1/1',periods=df.shape[0])# 添加一个日期索引 index
In [4]:
df=pd.DataFrame(np.random.rand(10,5))
df.index=pd.date_range('2017/1/1',periods=df.shape[0])
df
Out[4]:
0 1 2 3 4
2017-01-010.2485150.6478890.1113460.5404340.159914
2017-01-020.4450730.3298430.8236780.7374380.707598
2017-01-030.5265430.8768260.7179860.2719200.719657
2017-01-040.4712560.6576470.9734840.5989970.249301
2017-01-050.9584650.4743310.0040780.8423430.819295
2017-01-060.2713080.2719880.4347760.4496520.369188
2017-01-070.9895730.9284280.4524360.0585900.732283
2017-01-080.4353280.7302140.9094000.6834130.186820
2017-01-090.8974140.6875250.1229370.0181020.440427
2017-01-100.7438210.1346020.2103260.8771570.815462
数据的查看与检查
df.head(n)# 查看数据框的前n行
In [5]:
df=pd.DataFrame(np.random.rand(10,5))
df.head(3)
Out[5]:
01234
00.7058840.8458130.7705850.4810490.381055
10.7333090.5423630.2643340.2542830.859442
20.4979770.4748980.8060730.3844120.242989
df.tail(n)# 查看数据框的最后n行
In [6]:
df=pd.DataFrame(np.random.rand(10,5))
df.tail(3)
Out[6]:
01234
70.6172890.0098010.2201550.9927430.944472
80.2611410.9409250.0633940.0521040.517853
90.6345410.8974830.7484530.8058610.344938
df.shape# 查看数据框的行数与列数
In [7]:
df=pd.DataFrame(np.random.rand(10,5))
df.shape
Out[7]:
(10, 5)
df.info()# 查看数据框 (DataFrame) 的索引、数据类型及内存信息
In [8]:
df=pd.DataFrame(np.random.rand(10,5))
df.info()
RangeIndex: 10 entries, 0 to 9
Data columns (total 5 columns):
0 10 non-null float64
1 10 non-null float64
2 10 non-null float64
3 10 non-null float64
4 10 non-null float64
dtypes: float64(5)
memory usage: 480.0 bytes
df.describe()# 对于数据类型为数值型的列,查询其描述性统计的内容
In [9]:
df.describe()
Out[9]:
01234
count10.00000010.00000010.00000010.00000010.000000
mean0.4106310.4975850.5062000.3229600.603119
std0.2803300.3225730.2547800.2602990.256370
min0.0437310.0317420.0706680.0448220.143786
25%0.2406610.2116250.4168270.1452980.422969
50%0.3462970.5446970.4796480.2173590.635974
75%0.4931050.6690440.5573530.4681190.782573
max0.9375830.9455730.9873280.8831570.992891
s.value_counts(dropna=False)# 查询每个独特数据值出现次数统计
In [10]:
s=pd.Series([1,2,3,3,4,np.nan,5,5,5,6,7])
s.value_counts(dropna=False)
Out[10]:
5.0 3
3.0 2
7.0 1
6.0 1
NaN 1
4.0 1
2.0 1
1.0 1
dtype: int64
df.apply(pd.Series.value_counts)# 查询数据框 (Data Frame) 中每个列的独特数据值出现次数统计
数据的选取
df[col]# 以数组 Series 的形式返回选取的列
In [11]:
df=pd.DataFrame(np.random.rand(5,5),columns=list('ABCDE'))
df['C']
Out[11]:
0 0.720965
1 0.360155
2 0.474067
3 0.116206
4 0.774503
Name: C, dtype: float64
df[[col1,col2]]# 以新的数据框(DataFrame)的形式返回选取的列
In [12]:
df=pd.DataFrame(np.random.rand(5,5),columns=list('ABCDE'))
df[['B','E']]
Out[12]:
BE
00.2059120.333909
10.4756200.540206
20.1440410.065117
30.6369700.406317
40.4515410.944245
s.iloc[0]# 按照位置选取
In [13]:
s=pd.Series(np.array(['I','Love','Data']))
s.iloc[0]
Out[13]:
'I'
s.loc['index_one']# 按照索引选取
In [14]:
s=pd.Series(np.array(['I','Love','Data']))
s.loc[1]
Out[14]:
'Love'
df.iloc[0,:]# 选取第一行
In [15]:
df=pd.DataFrame(np.random.rand(5,5),columns=list('ABCDE'))
df.iloc[0,:]
Out[15]:
A 0.234156
B 0.513754
C 0.593067
D 0.856575
E 0.291528
Name: 0, dtype: float64
df.iloc[0,0]# 选取第一行的第一个元素
In [16]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.iloc[0,0]
Out[16]:
0.91525996455410763
数据的清洗
df.columns=['a','b']# 重命名数据框的列名称
In [17]:
df=pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),'B':np.array([np.nan,4,np.nan,5,9,np.nan]),'C':'foo'})
df.columns=['a','b','c']
df
Out[17]:
abc
01.0NaNfoo
1NaN4.0foo
22.0NaNfoo
33.05.0foo
46.09.0foo
5NaNNaNfoo
pd.isnull()# 检查数据中空值出现的情况,并返回一个由布尔值(True,Fale)组成的列
In [18]:
df=pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),'B':np.array([np.nan,4,np.nan,5,9,np.nan]),'C':'foo'})
pd.isnull(df)
Out[18]:
ABC
0FalseTrueFalse
1TrueFalseFalse
2FalseTrueFalse
3FalseFalseFalse
4FalseFalseFalse
5TrueTrueFalse
pd.notnull()# 检查数据中非空值出现的情况,并返回一个由布尔值(True,False)组成的列
In [19]:
df=pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),'B':np.array([np.nan,4,np.nan,5,9,np.nan]),'C':'foo'})
pd.notnull(df)
Out[19]:
ABC
0TrueFalseTrue
1FalseTrueTrue
2TrueFalseTrue
3TrueTrueTrue
4TrueTrueTrue
5FalseFalseTrue
df.dropna()# 移除数据框 DataFrame 中包含空值的行
In [20]:
df=pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),'B':np.array([np.nan,4,np.nan,5,9,np.nan]),'C':'foo'})
df.dropna()
Out[20]:
ABC
33.05.0foo
46.09.0foo
df.dropna(axis=1)# 移除数据框 DataFrame 中包含空值的列
In [21]:
df=pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),'B':np.array([np.nan,4,np.nan,5,9,np.nan]),'C':'foo'})
df.dropna(axis=1)
Out[21]:
C
0foo
1foo
2foo
3foo
4foo
5foo
df.dropna(axis=0,thresh=n)
In [22]:
df=pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),'B':np.array([np.nan,4,np.nan,5,9,np.nan]),'C':'foo'})
test=df.dropna(axis=1,thresh=1)
test
Out[22]:
ABC
01.0NaNfoo
1NaN4.0foo
22.0NaNfoo
33.05.0foo
46.09.0foo
5NaNNaNfoo
df.fillna(x)# 将数据框 DataFrame 中的所有空值替换为 x
In [23]:
df=pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),'B':np.array([np.nan,4,np.nan,5,9,np.nan]),'C':'foo'})
df.fillna('Test')
Out[23]:
ABC
01Testfoo
1Test4foo
22Testfoo
335foo
469foo
5TestTestfoo
s.fillna(s.mean()) -> 将所有空值替换为平均值
In [24]:
s=pd.Series([1,3,5,np.nan,7,9,9])
s.fillna(s.mean())
Out[24]:
0 1.000000
1 3.000000
2 5.000000
3 5.666667
4 7.000000
5 9.000000
6 9.000000
dtype: float64
s.astype(float)# 将数组(Series)的格式转化为浮点数
In [25]:
s=pd.Series([1,3,5,np.nan,7,9,9])
s.astype(float)
Out[25]:
0 1.0
1 3.0
2 5.0
3 NaN
4 7.0
5 9.0
6 9.0
dtype: float64
s.replace(1,'one')# 将数组(Series)中的所有1替换为'one'
In [26]:
s=pd.Series([1,3,5,np.nan,7,9,9])s.replace(1,'one')
Out[26]:
0 one
1 3
2 5
3 NaN
4 7
5 9
6 9
dtype: object
s.replace([1,3],['one','three'])# 将数组(Series)中所有的1替换为'one', 所有的3替换为'three'
In [27]:
s=pd.Series([1,3,5,np.nan,7,9,9])
s.replace([1,3],['one','three'])
Out[27]:
0 one
1 three
2 5
3 NaN
4 7
5 9
6 9
dtype: object
df.rename(columns=lambdax:x+2)# 将全体列重命名
In [28]:
df=pd.DataFrame(np.random.rand(4,4))
df.rename(columns=lambdax:x+2)
Out[28]:
2345
00.7535880.1379840.0220130.900072
10.9470730.8151820.7697080.729688
20.3348150.2043150.7077940.437704
30.4672120.7383600.8534630.529946
df.rename(columns={'old_name':'new_ name'})# 将选择的列重命名
In [29]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.rename(columns={'A':'newA','C':'newC'})
Out[29]:
newABnewCDE
00.1690720.6945630.0693130.6375600.475181
10.9102710.8000670.6764480.9347670.025608
20.8251860.4515450.1354210.6353030.419758
30.4019790.5103040.0149010.2092110.121889
40.5792820.0019470.0365190.7504150.453078
50.8962130.5575140.0281470.5274710.575772
60.4432220.0954590.3195820.9120690.781455
70.0679230.5904700.6029990.5073580.703022
80.3014910.6826290.2831030.5657540.089268
90.3996710.9254160.0205780.2780000.591522
df.set_index('column_one')# 改变索引
In [30]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.set_index('B')
Out[30]:
ACDE
B
0.3117420.9720690.5579770.1142670.795128
0.9316440.7254250.0821300.9937640.136923
0.2063820.9806470.9470410.0388410.879139
0.1578010.4022330.2491510.7241300.108238
0.3142380.3412210.5121800.2188820.046379
0.0290400.4706190.6667840.0366550.823498
0.8439280.7794370.9269120.1892130.624111
0.2827730.9936810.0484830.1359340.576662
0.7596000.2355130.3591390.4882550.669043
0.0885520.8932690.2772960.8895230.398392
df.rename(index=lambdax:x+1)# 改变全体索引
In [31]:
df=pd.DataFrame(np.random.rand(10,5))
df.rename(index=lambdax:x+1)
Out[31]:
01234
10.3865420.0319320.9632000.7903390.602533
20.0534920.6521740.8894650.4652960.843528
30.4118360.4607880.1103520.0832470.389855
40.3361560.8305220.5609910.6678960.233841
50.3079330.9952070.5066800.9578950.636461
60.7249750.8421180.1231390.2443570.803936
70.0591760.1177840.3301920.4187640.464144
80.1043230.2223670.9304140.6592320.562155
90.4840890.0240450.8798340.4922310.949636
100.2015830.2806580.3568040.8907060.236174
数据的过滤(filter),排序(sort)和分组(groupby)
df[df[col]>0.5]# 选取数据框df中对应行的数值大于0.5的全部列
In [32]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df[df['A']>0.5]
Out[32]:
ABCDE
00.5348860.8635460.2367180.3267660.415460
20.9539310.0701980.4837490.9225280.295505
80.8801750.0568110.5204990.5331520.548145
df[(df[col]>0.5)&(df[col]<0.7)]# 选取数据框df中对应行的数值大于0.5,并且小于0.7的全部列
In [33]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df[(df['C']>0.5)&(df['D']<0.7)]
Out[33]:
ABCDE
20.9531120.1745170.6453000.3082160.171177
60.8530870.8630790.7018230.3540190.311754
df.sort_values(col1)# 按照数据框的列col1升序(ascending)的方式对数据框df做排序
In [34]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.sort_values('E')
Out[34]:
ABCDE
30.0240960.6238420.7759490.8283430.317729
60.2200550.3816140.4636760.7626440.391758
40.5894110.7274390.0645280.3195210.413518
10.8784900.2293010.6995060.7268790.464106
80.4381010.9706490.0502560.6974400.499057
90.5661000.5587980.7232530.2542440.524486
70.6136030.9331090.6770360.8081600.544953
50.0793260.7116730.2664340.9106280.816783
20.1321140.1453950.9084360.5212710.889645
00.4326770.2168370.2035320.0932140.977671
df.sort_values(col2,ascending=False)# 按照数据框的列col2降序(descending)的方式对数据框df做排序
In [35]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.sort_values('A',ascending=False)
Out[35]:
ABCDE
90.9771720.9306070.8892850.4750320.031715
00.8645110.2299900.6786120.0424910.148123
20.6947470.5808910.8175240.3924170.055003
60.6843270.8020280.8620430.2418380.800401
70.6123240.0994450.7141200.2150540.280343
80.4414340.3155530.5647620.8001430.330030
10.4387340.1611090.6107500.6473300.792404
40.3658800.7107680.3443200.9987570.979497
30.2025110.7697280.5750570.5113840.696753
50.0295270.5601140.2247870.0862910.318322
df.sort_values([col1,col2],ascending=[True,False])# 按照数据框的列col1升序,col2降序的方式对数据框df做排序
In [36]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.sort_values(['A','E'],ascending=[True,False])
Out[36]:
ABCDE
60.0758630.6969800.6489450.3369770.113122
20.1993160.6320630.7873580.1331750.060568
50.2420810.8185500.6184390.2157610.924459
70.2612370.4007250.6592240.5557460.132572
00.3905400.3584320.7540280.1944030.889624
80.4104810.4638110.3430210.7363400.291121
40.5787050.5447110.8817070.3965930.414465
30.6005410.4592470.5913030.0274640.496864
90.7200290.4199210.7402250.9043910.226958
10.7779550.9922900.1444950.6002070.647018
df.groupby(col)# 按照某列对数据框df做分组
In [37]:
df=pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),'B':np.array(['one','one','two','two','three','three']),'C':np.array(['small','medium','large','large','small','small']),'D':np.array([1,2,2,3,3,5])})
df.groupby('A').count()
Out[37]:
BCD
A
bar222
foo444
df.groupby([col1,col2])# 按照列col1和col2对数据框df做分组
In [38]:
df=pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),'B':np.array(['one','one','two','two','three','three']),'C':np.array(['small','medium','large','large','small','small']),'D':np.array([1,2,2,3,3,5])})
df.groupby(['B','C']).sum()
Out[38]:
D
BC
onemedium2
small1
threesmall8
twolarge5
df.groupby(col1)[col2].mean()# 按照列col1对数据框df做分组处理后,返回对应的col2的平均值
In [39]:
df=pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),'B':np.array(['one','one','two','two','three','three']),'C':np.array(['small','medium','large','large','small','small']),'D':np.array([1,2,2,3,3,5])})
df.groupby('B')['D'].mean()
Out[39]:
B
one 1.5
three 4.0
two 2.5
Name: D, dtype: float64
pythyon
df.pivot_table(index=col1,values=[col2,col3],aggfunc=mean) # 做透视表,索引为col1,针对的数值列为col2和col3,分组函数为平均值
In [40]:
df=pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),'B':np.array(['one','one','two','two','three','three']),'C':np.array(['small','medium','large','large','small','small']),'D':np.array([1,2,2,3,3,5])})
df.pivot_table(df,index=['A','B'],columns=['C'],aggfunc=np.sum)
Out[40]:
D
Clargemediumsmall
AB
barthreeNaNNaN8.0
foooneNaN2.01.0
two5.0NaNNaN
df.groupby(col1).agg(np.mean)
In [41]:
df=pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),'B':np.array(['one','one','two','two','three','three']),'C':np.array(['small','medium','large','large','small','small']),'D':np.array([1,2,2,3,3,5])})
df.groupby('A').agg(np.mean)
Out[41]:
D
A
bar4
foo2
df.apply(np.mean)# 对数据框df的每一列求平均值
In [42]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.apply(np.mean)
Out[42]:
A 0.388075
B 0.539564
C 0.607983
D 0.518634
E 0.482960
dtype: float64
df.apply(np.max,axis=1)# 对数据框df的每一行求最大值
In [43]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.apply(np.max,axis=1)
Out[43]:
0 0.904163
1 0.804519
2 0.924102
3 0.761781
4 0.952084
5 0.923679
6 0.796320
7 0.582907
8 0.761310
9 0.893564
dtype: float64
数据的连接(join)与组合(combine)
df1.append(df2)# 在数据框df2的末尾添加数据框df1,其中df1和df2的列数应该相等
In [44]:
df1=pd.DataFrame({'A':['A0','A1','A2','A3'],'B':['B0','B1','B2','B3'],'C':['C0','C1','C2','C3'],'D':['D0','D1','D2','D3']},index=[0,1,2,3])df2=pd.DataFrame({'A':['A4','A5','A6','A7'],'B':['B4','B5','B6','B7'],'C':['C4','C5','C6','C7'],'D':['D4','D5','D6','D7']},index=[4,5,6,7])
df1.append(df2)
Out[44]:
ABCD
0A0B0C0D0
1A1B1C1D1
2A2B2C2D2
3A3B3C3D3
4A4B4C4D4
5A5B5C5D5
6A6B6C6D6
7A7B7C7D7
pd.concat([df1,df2],axis=1)# 在数据框df1的列最后添加数据框df2,其中df1和df2的行数应该相等
In [45]:
df1=pd.DataFrame({'A':['A0','A1','A2','A3'],'B':['B0','B1','B2','B3'],'C':['C0','C1','C2','C3'],'D':['D0','D1','D2','D3']},index=[0,1,2,3])df2=pd.DataFrame({'A':['A4','A5','A6','A7'],'B':['B4','B5','B6','B7'],'C':['C4','C5','C6','C7'],'D':['D4','D5','D6','D7']},index=[4,5,6,7])
pd.concat([df1,df2],axis=1)
Out[45]:
ABCDABCD
0A0B0C0D0NaNNaNNaNNaN
1A1B1C1D1NaNNaNNaNNaN
2A2B2C2D2NaNNaNNaNNaN
3A3B3C3D3NaNNaNNaNNaN
4NaNNaNNaNNaNA4B4C4D4
5NaNNaNNaNNaNA5B5C5D5
6NaNNaNNaNNaNA6B6C6D6
7NaNNaNNaNNaNA7B7C7D7
df1.join(df2,on=col1,how='inner')# 对数据框df1和df2做内连接,其中连接的列为col1
In [46]:
df1=pd.DataFrame({'A':['A0','A1','A2','A3'],'B':['B0','B1','B2','B3'],'key':['K0','K1','K0','K1']})df2=pd.DataFrame({'C':['C0','C1'],'D':['D0','D1']},index=['K0','K1'])df1.join(df2,on='key')
Out[46]:
ABkeyCD
0A0B0K0C0D0
1A1B1K1C1D1
2A2B2K0C0D0
3A3B3K1C1D1
数据的统计
df.describe()# 得到数据框df每一列的描述性统计
In [47]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.describe()
Out[47]:
ABCDE
count10.00000010.00000010.00000010.00000010.000000
mean0.3986480.4516990.4434720.7394780.412954
std0.3306050.2215860.3030840.3087980.262148
min0.0044570.1886890.0796970.1135620.052935
25%0.0881770.2703550.2056630.7150050.205685
50%0.3155330.4572290.3321480.8858720.400232
75%0.7497160.4972080.7379000.9486510.634670
max0.7829560.8256710.8510650.9629220.815447
df.mean()# 得到数据框df中每一列的平均值
In [48]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.mean()
Out[48]:
A 0.395643
B 0.528812
C 0.692011
D 0.446750
E 0.544759
dtype: float64
df.corr()# 得到数据框df中每一列与其他列的相关系数
In [49]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.corr()
Out[49]:
ABCDE
A1.000000-0.634931-0.354824-0.3541310.170957
B-0.6349311.0000000.225222-0.338124-0.043300
C-0.3548240.2252221.0000000.0982850.297133
D-0.354131-0.3381240.0982851.000000-0.324209
E0.170957-0.0433000.297133-0.3242091.000000
df.count()# 得到数据框df中每一列的非空值个数
In [50]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.count()
Out[50]:
A 10
B 10
C 10
D 10
E 10
dtype: int64
df.max()# 得到数据框df中每一列的最大值
In [51]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.max()
Out[51]:
A 0.933848
B 0.730197
C 0.921751
D 0.715280
E 0.940010
dtype: float64
df.min()# 得到数据框df中每一列的最小值
In [52]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.min()
Out[52]:
A 0.107516
B 0.001635
C 0.024502
D 0.092810
E 0.019898
dtype: float64
df.median()# 得到数据框df中每一列的中位数
In [53]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.median()
Out[53]:
A 0.497591
B 0.359854
C 0.661607
D 0.342418
E 0.588468
dtype: float64
df.std()# 得到数据框df中每一列的标准差
In [54]:
df=pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))
df.std()
Out[54]:
A 0.231075
B 0.286691
C 0.276511
D 0.304167
E 0.272570
dtype: float64