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) '''
pandas series and datarame align操作
最新推荐文章于 2024-08-26 09:27:29 发布