pandas series and datarame align操作

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

'''The align() method is the fastest way to simultaneously align two objects. It supports a join argument 
(related to joining and merging):

join='outer': take the union of the indexes (default)
join='left': use the calling object’s index
join='right': use the passed object’s index
join='inner': intersect the indexes
It returns a tuple with both of the reindexed Series:'''


#Series操作
s=pd.Series(np.random.randn(5),index=list('abcde'))
s1=s[:4]
s2=s[1:]
# print(s1)
# print(s2)
tt=s1.align(s2)
# print(type(s1.align(s2))) #<class 'tuple'>
# print(list(tt)[0])
# print(type(list(tt)[0]))  #<class 'pandas.core.series.Series'>
# print(s1.align(s2, join='inner'))
# print('*'*44)
# print(s1.align(s2, join='left'))
# print('*'*44)
# print(s1.align(s2, join='right'))
# print('*'*44)

'''For DataFrames, the join method will be applied to both the index and the columns by default:'''

df=pd.DataFrame({
    'one':pd.Series(np.random.randn(3),index=list('abc')),
    'two':pd.Series(np.random.randn(4),index=list('abcd')),
    'three':pd.Series(np.random.randn(3),index=list('bcd'))
},columns=['one','two','three'])

df2=pd.DataFrame({
    'one':pd.Series(np.random.randn(3),index=list('abc')),
    'two':pd.Series(np.random.randn(3),index=list('abc'))
})
print(list(df.align(df2))[1])
print('*'*44)
'''        one  three       two
a  1.157157    NaN  0.445614
b -0.299403    NaN  0.262753
c -0.537562    NaN  0.127325
d       NaN    NaN       NaN'''

print(df.align(df2,join='right'))
'''(        one       two
a  0.608164 -1.179006
b  1.169596  2.313169
c  1.044233  0.481608,         one       two
a -0.965940 -1.640197
b  1.185849 -0.166843
c  1.409709  0.497200)
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值