pandasの数学計算操作df.sum()、df.min()、df.max()、df.decribe()

11755 ワード

よく使われる数学計算は加減,最大値最小値,分散などにほかならないが,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())) # 
'''
        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
'''