Pandas玩转数据(一) -- 简单计算

数据分析汇总学习

https://blog.csdn.net/weixin_39778570/article/details/81157884

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

# Series 计算 可以计算加减乘,这里以加法为例
s1 = Series([1,2,3], index=['B','C','D'])
s2 = Series([4,5,6,7], index=['B','C','D','E'])
# 没有的数据为nan
s1 + s2
Out[10]: 
B    5.0
C    7.0
D    9.0
E    NaN
dtype: float64

# DataFrame计算,可加减乘
df1 = DataFrame(np.arange(4).reshape(2,2), index=['A','B'], columns=['BJ','GZ'])
df1
Out[13]: 
   BJ  GZ
A   0   1
B   2   3

df2 = DataFrame(np.arange(9).reshape(3,3), index=['A','B','C'], columns=['BJ', 'GZ', 'SH'])
df2
Out[15]: 
   BJ  GZ  SH
A   0   1   2
B   3   4   5
C   6   7   8

df1+df2
Out[16]: 
    BJ   GZ  SH
A  0.0  2.0 NaN
B  5.0  7.0 NaN
C  NaN  NaN NaN

# DataFrame相关函数
df3 = DataFrame([[1,2,3],[4,5,np.nan],[7,8,9]], index=['A','B','C'], columns=['c1','c2','c3'])

df3
Out[19]: 
   c1  c2   c3
A   1   2  3.0
B   4   5  NaN
C   7   8  9.0
# 列和
df3.sum()
Out[20]: 
c1    12.0
c2    15.0
c3    12.0
dtype: float64
# 行和
df3.sum(axis=1)
Out[21]: 
A     6.0
B     9.0
C    24.0
dtype: float64
# 最大值与最小值
df3.max()
Out[22]: 
c1    7.0
c2    8.0
c3    9.0
dtype: float64

df3.max(axis=1)
Out[23]: 
A    3.0
B    5.0
C    9.0
dtype: float64

df3.min()
Out[24]: 
c1    1.0
c2    2.0
c3    3.0
dtype: float64

df3.min(axis=1)
Out[25]: 
A    1.0
B    4.0
C    7.0
dtype: float64

# describe描述
df3.describe()
Out[26]: 
        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
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页