pandas-12 数学计算操作df.sum()、df.min()、df.max()、df.decribe()
常用的数学计算无非就是加减,最大值最小值,方差等等,pandas已经内置了很多方法来解决这些问题。如:df.sum()、df.min()、df.max()、df.decribe()等。
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
s1 = Series([1, 2, 3], index=['A', 'B', 'C'])
print(s1)
'''
A 1
B 2
C 3
dtype: int64
'''
s2 = Series([4, 5, 6, 7], index=['B', 'C', 'D', 'E'])
print(s2)
'''
B 4
C 5
D 6
E 7
dtype: int64
'''
# 两个series进行相加,任何数字和nan相加都是nan
print(s1 + s2)
'''
A NaN
B 6.0
C 8.0
D NaN
E NaN
dtype: float64
'''
# dataframe 的规则和series一样
df1 = DataFrame(np.arange(4).reshape(2, 2), index=['A', 'B'], columns=['bj', 'sh'])
print(df1)
'''
bj sh
A 0 1
B 2 3
'''
df2 = DataFrame(np.arange(9).reshape(3, 3), index=['A', 'B', 'C'], columns=['bj', 'sh', 'gz'])
print(df2)
'''
bj sh gz
A 0 1 2
B 3 4 5
C 6 7 8
'''
print(df1 + df2)
'''
bj gz sh
A 0.0 NaN 2.0
B 5.0 NaN 7.0
C NaN NaN NaN
'''
df3 = DataFrame([[1, 2, 3], [4, 5, np.nan], [7, 8, 9]], index=['A', 'B', 'C'], columns=['c1', 'c2', 'c3'])
print(df3)
'''
c1 c2 c3
A 1 2 3.0
B 4 5 NaN
C 7 8 9.0
'''
# 按照每一列 相加,返回
print(df3.sum())
'''
c1 12.0
c2 15.0
c3 12.0
dtype: float64
'''
# 指定 axis, 按照每一行相加
print(df3.sum(axis=1))
'''
A 6.0
B 9.0
C 24.0
dtype: float64
'''
print(df3.min())
'''
c1 1.0
c2 2.0
c3 3.0
dtype: float64
'''
print(df3.min(axis=1))
'''
A 1.0
B 4.0
C 7.0
dtype: float64
'''
print(df3.max())
# 打印出dataframe的数学信息
print(df3.describe())
print(type(df3.describe())) # <class 'pandas.core.frame.DataFrame'>
'''
c1 c2 c3
count 3.0 3.0 2.000000
mean 4.0 5.0 6.000000
std 3.0 3.0 4.242641
min 1.0 2.0 3.000000
25% 2.5 3.5 4.500000
50% 4.0 5.0 6.000000
75% 5.5 6.5 7.500000
max 7.0 8.0 9.000000
'''