栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas的数据对齐

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas的数据对齐

import pandas as pd
import numpy as np

1、Series数据对齐
ps1 = pd.Series(np.arange(4),index=['a','b','c','d'])
ps2 = pd.Series(np.arange(5),index=['a','c','e','f','g'])
ps1
a    0
b    1
c    2
d    3
dtype: int32
ps2
a    0
c    1
e    2
f    3
g    4
dtype: int32
print(ps1+ps2)
ps1.add(ps2,fill_value = 0)
a    0.0
b    NaN
c    3.0
d    NaN
e    NaN
f    NaN
g    NaN
dtype: float64
a    0.0
b    1.0
c    3.0
d    3.0
e    2.0
f    3.0
g    4.0
dtype: float64

Datafram 数据对齐
#Datafram 数据对齐
pd1 = pd.Dataframe(np.arange(12).reshape(4,3),index=['a','b','c','d'],columns=['A','B','C'])
pd2 = pd.Dataframe(np.arange(9).reshape(3,3),index=['a','d','f'],columns=['A','B','D'])
pd1
 ABC
a012
b345
c678
d91011
pd2
 ABD
a012
d345
f678
pd1+pd2
 ABCD
a0.02.0NaNNaN
bNaNNaNNaNNaN
cNaNNaNNaNNaN
d12.014.0NaNNaN
fNaNNaNNaNNaN
print('*******处理缺失值*******')
pd1.add(pd2,fill_value = 0)
*******处理缺失值*******
 ABCD
a0.02.02.02.0
b3.04.05.0NaN
c6.07.08.0NaN
d12.014.011.05.0
f6.07.0NaN8.0

混合运算
s3 = pd2.iloc[0] 
s3
A    0
B    1
D    2
Name: a, dtype: int32
pd2
 ABD
a012
d345
f678
pd2-s3
 ABD
a000
d333
f666

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/303924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号