DataFrame、Series运算

初始配置

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

# 下面两个方法都可以
frame = DataFrame(np.arange(9).reshape(3,3), columns=['a','b','c'], index=['one', 'two', 'threee'])
# print(frame)

series = frame['b']
# print(series)

查看数据

In[3]: frame
Out[3]: 
        a  b  c
one     0  1  2
two     3  4  5
threee  6  7  8

In[4]: series
Out[4]: 
one       1
two       4
threee    7
Name: b, dtype: int32

DataFrame与Series运算

In[5]: frame.add(series, axis=0) #每列都加series
Out[5]: 
         a   b   c
one      1   2   3
two      7   8   9
threee  13  14  15

In[6]: frame.sub(series, axis=0)
Out[6]: 
        a  b  c
one    -1  0  1
two    -1  0  1
threee -1  0  1

In[7]: frame.mul(series, axis=0)
Out[7]: 
         a   b   c
one      0   1   2
two     12  16  20
threee  42  49  56

In[8]: frame.div(series, axis=0)
Out[8]: 
               a    b         c
one     0.000000  1.0  2.000000
two     0.750000  1.0  1.250000
threee  0.857143  1.0  1.142857

Series之间的运算(最初的frame)

s1 = frame['b'] #1,4,7
s2 = frame['c'] #2,5,8
s1.add(s2) #加
s1.sub(s2) #减
s1.mul(s2) #乘
s1.div(s2) #除

Series自身

#求中位数
s.median()
#求和
s.sum()
#求最大值
s.max()
#求最小值
s.min()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值