1.count() # 非空观测数量
2.sum() # 所有值之和
3.mean() # 所有值的平均值
4.median() # 所有值的中位数
5.mode() # 值的模值
6.std() # 值的标准偏差
7.min() # 所有值中的最小值
8.max() # 所有值中的最大值
9.abs() # 绝对值
10.prod() # 数组元素的乘积
11.cumsum() # 累计总和
12.cumprod() # 累计乘积
import pandas as pd
# Create a Dictionary of series
d = {'Name': pd.Series(['Tom', 'James', 'Ricky', 'Vin', 'Steve', 'Minsu', 'Jack', 'Lee', 'David', 'Gasper', 'Betina', 'Andres']),
'Age': pd.Series([25, 26, 25, 23, 30, 29, 23, 34, 40, 30, 51, 46]),
'Rating': pd.Series([4.23, 3.24, 3.98, 2.56, 3.20, 4.6, 3.8, 3.78, 2.98, 4.80, 4.10, 3.65])}
# Create a DataFrame
data = pd.DataFrame(d)
print(data)
print("求和------------")
print(data.sum())
print("平均值------------")
print(data.mean())
print("值的标准偏差----------")
print(data.std())
结果:
D:\Python36\python.exe C:/Users/17653/Desktop/测试.py
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Ricky 25 3.98
3 Vin 23 2.56
4 Steve 30 3.20
5 Minsu 29 4.60
6 Jack 23 3.80
7 Lee 34 3.78
8 David 40 2.98
9 Gasper 30 4.80
10 Betina 51 4.10
11 Andres 46 3.65
求和------------
Name TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe...
Age 382
Rating 44.92
dtype: object
平均值------------
Age 31.833333
Rating 3.743333
dtype: float64
值的标准偏差----------
Age 9.232682
Rating 0.661628
dtype: float64
Process finished with exit code 0
Pandas 描述性统计函数,注意事项:
– 由于DataFrame是异构数据结构。通用操作不适用于所有函数。
– 类似于:sum(),cumsum()函数能与数字和字符(或)字符串数据元素一起工作,不会产生任何错误。
– 由于这样的操作无法执行,因此,当DataFrame包含字符或字符串数据时,像abs(),cumprod()这样的函数会抛出异常。
要将自定义或其他库的函数应用于Pandas对象,有三种方式:
– pipe():表格函数应用,通过将函数和适当数量的参数作为管道参数来执行自定义操作,对整个DataFrame执行操作。
– apply( ) :可以沿DataFrame的轴应用任意函数,它与描述性统计方法一样,采用可选的axis参数。
– applymap() :给DataFrame的所有元素应用任何Python函数,并且返回单个值。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3'])
print(df)
df = df.apply(np.max)
print(df)
结果:
D:\Python36\python.exe C:/Users/17653/Desktop/测试.py
col1 col2 col3
0 -0.724690 1.490370 -0.211312
1 0.134439 -0.675743 0.241889
2 -2.220712 0.277121 0.286439
3 -1.465223 -0.533942 1.471343
4 -1.342505 0.883603 0.791163
col1 0.134439
col2 1.490370
col3 1.471343
dtype: float64
Process finished with exit code 0
dataframe获取所有行之和大于100的数据, 并返回最后的两行
筛选某列大于某数
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(10, 40, 60).reshape(-1, 4))
print(df)
print("获取所有行之和大于100的数据, 并返回最后的两行----------")
rowsums = df.apply(np.sum, axis=1) # axis=1 纵坐标,axis=0横坐标
print(rowsums)
last_two_rows = df.iloc[np.where(rowsums > 100)[0][-2:]]
print(last_two_rows)
# 指定3列数据大于30
print('指定第3列数据大于30--------------------------')
last_two_rows = df.iloc[np.where(df[3] > 30)[0][:]]
print(last_two_rows)
结果:
D:\Python36\python.exe C:/Users/17653/Desktop/测试.py
0 1 2 3
0 21 12 38 29
1 21 39 16 17
2 32 14 16 14
3 37 34 12 33
4 11 34 22 30
5 35 32 20 23
6 12 34 38 23
7 10 14 12 22
8 14 36 24 10
9 22 29 15 26
10 37 25 11 23
11 39 32 17 19
12 39 26 16 13
13 22 20 12 39
14 14 32 29 34
获取所有行之和大于100的数据, 并返回最后的两行----------
0 100
1 93
2 76
3 116
4 97
5 110
6 107
7 58
8 84
9 92
10 96
11 107
12 94
13 93
14 109
dtype: int64
0 1 2 3
11 39 32 17 19
14 14 32 29 34
指定第3列数据大于30--------------------------
0 1 2 3
3 37 34 12 33
13 22 20 12 39
14 14 32 29 34
Process finished with exit code 0
Pandas对象之间的基本迭代的行为取决于类型。当迭代一个系列时,它 被视为数组式,基本迭代产生这些值
import pandas as pd
import numpy as np
N = 5
df = pd.DataFrame({
'X': np.linspace(0, stop=N - 1, num=N),
'Y': np.random.rand(N),
'C': np.random.choice(['Low', 'Medium', 'High'], N)
.tolist(),
})
print(df)
print("按列访问值=====================")
for key, value in df.iteritems(): # 按列访问值
print(key, value)
print("按行访问值=====================")
for row_index, row in df.iterrows(): # 按行访问值
print(row_index, row)
print("按行访问值=====================")
for row in df.itertuples(): # 按行访问值
print(row)
结果:
D:\Python36\python.exe C:/Users/17653/Desktop/测试.py
X Y C
0 0.0 0.963108 High
1 1.0 0.920592 High
2 2.0 0.853316 High
3 3.0 0.550055 High
4 4.0 0.042461 High
按列访问值=====================
X 0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
Name: X, dtype: float64
Y 0 0.963108
1 0.920592
2 0.853316
3 0.550055
4 0.042461
Name: Y, dtype: float64
C 0 High
1 High
2 High
3 High
4 High
Name: C, dtype: object
按行访问值=====================
0 X 0
Y 0.963108
C High
Name: 0, dtype: object
1 X 1
Y 0.920592
C High
Name: 1, dtype: object
2 X 2
Y 0.853316
C High
Name: 2, dtype: object
3 X 3
Y 0.550055
C High
Name: 3, dtype: object
4 X 4
Y 0.0424612
C High
Name: 4, dtype: object
按行访问值=====================
Pandas(Index=0, X=0.0, Y=0.9631079257728457, C='High')
Pandas(Index=1, X=1.0, Y=0.9205919970333097, C='High')
Pandas(Index=2, X=2.0, Y=0.8533157659933952, C='High')
Pandas(Index=3, X=3.0, Y=0.5500551649308855, C='High')
Pandas(Index=4, X=4.0, Y=0.042461183897673616, C='High')
Process finished with exit code 0
Pandas中有两种排序方式:
– 按标签排序:sort_index()方法通过传递axis参数和排序顺序,可以对DataFrame进行排序。ascending=true为升序,false为降序。axis=0排序行,1为排序列。
– 按实际值:sort_values()是按值排序的方法。它接受一个by参数,指定排序列名
import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame(np.random.randn(10, 2),
index=[1, 4, 6, 2, 3, 5, 9, 8, 0, 7],
columns=['col2', 'col1'])
print(unsorted_df)
sorted_df = unsorted_df.sort_index()
print(sorted_df) # 按索引排序
sorted_df = unsorted_df.sort_values(by='col1')
print(sorted_df) # 按col1排序
结果:
D:\Python36\python.exe C:/Users/17653/Desktop/测试.py
col2 col1
1 1.316384 0.247048
4 0.982346 -0.780983
6 0.398843 -0.445167
2 1.332114 -1.169454
3 -0.468013 0.434808
5 0.017994 0.896905
9 1.596637 0.021462
8 -1.343919 0.001835
0 0.245334 -1.820004
7 -0.091836 1.131269
col2 col1
0 0.245334 -1.820004
1 1.316384 0.247048
2 1.332114 -1.169454
3 -0.468013 0.434808
4 0.982346 -0.780983
5 0.017994 0.896905
6 0.398843 -0.445167
7 -0.091836 1.131269
8 -1.343919 0.001835
9 1.596637 0.021462
col2 col1
0 0.245334 -1.820004
2 1.332114 -1.169454
4 0.982346 -0.780983
6 0.398843 -0.445167
8 -1.343919 0.001835
9 1.596637 0.021462
1 1.316384 0.247048
3 -0.468013 0.434808
5 0.017994 0.896905
7 -0.091836 1.131269
Process finished with exit code 0
Pandas函数应用:
常用字符串文本函数列表如下: