from pandas importdataframe,series
import
pandas as pd
import
numpy as np
arr = np.arange(12).reshape((3,4))
(arr)
'''[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
'''print(np.concatenate([arr,arr],axis=1))
'''[[ 0 1 2 3 0 1 2 3]
[ 4 5 6 7 4 5 6 7]
[ 8 9 10 11 8 9 10 11]]
'''s1 = series([0,1],index=['
a','b'
])s2 = series([2,3,4],index=['
c','
d','e'
])s3 = series([5,6],index=['
f','g'
(s1)
'''a 0
b 1
dtype: int64
(s2)
'''c 2
d 3
e 4
dtype: int64
(s3)
'''f 5
g 6
dtype: int64
(pd.concat([s1,s2,s3]))
'''a 0
b 1
c 2
d 3
e 4
f 5
g 6
dtype: int64
'''#
concat是在axis=0上工作的,產生乙個新的series,如果傳入axis=1,結果會變成dataframe(axis=1是列)
print(pd.concat([s1,s2,s3],axis=1,sort=true))
'''0 1 2
a 0.0 nan nan
b 1.0 nan nan
c nan 2.0 nan
d nan 3.0 nan
e nan 4.0 nan
f nan nan 5.0
g nan nan 6.0
'''s4 = series([0,1],index=['
a','b'
])s5 = series([2,3,4],index=['
a','
b','c'
])print(pd.concat([s4,s5])) #
軸向鏈結
'''a 0
b 1
a 2
b 3
c 4
dtype: int64
'''print(pd.concat([s4,s5],axis=1,sort=true)) #
外連線,並集
'''0 1
a 0.0 2
b 1.0 3
c nan 4
'''print(pd.concat([s4,s5],axis=1,join='
inner
')) #
內連線,交集
'''0 1
a 0 2
b 1 3
'''print(pd.concat([s4,s5],axis=1,join_axes=[['
a','
b','
c','
d']])) #
使用join_axes指定索引
'''0 1
a 0.0 2.0
b 1.0 3.0
c nan 4.0
d nan nan
'''print(pd.concat([s1,s2,s3],keys=['
a','
b','
c'])) #
連線的片段在結果可使用keys區分
'''a a 0
b 1
b c 2
d 3
e 4
c f 5
g 6
dtype: int64
'''#
如果axis=1,keys就會變成dataframe的列頭
print(pd.concat([s1,s2,s3],keys=['
a','
b','
c'],axis=1,sort=true))
'''a b c
a 0.0 nan nan
b 1.0 nan nan
c nan 2.0 nan
d nan 3.0 nan
e nan 4.0 nan
f nan nan 5.0
g nan nan 6.0
'''df1 = dataframe(np.arange(6).reshape((3,2)),index=['
a','
b','
c'],columns=['
one','
two'
])df2 = dataframe(np.arange(4).reshape((2,2)),index=['
a','
c'],columns=['
three
','four'])
(df1)
'''one two
a 0 1
b 2 3
c 4 5
(df2)
'''three four
a 0 1
c 2 3
'''print(pd.concat([df1,df2],sort=true))
'''four one three two
a nan 0.0 nan 1.0
b nan 2.0 nan 3.0
c nan 4.0 nan 5.0
a 1.0 nan 0.0 nan
c 3.0 nan 2.0 nan
'''print(pd.concat([df1,df2],axis=1,sort=true))
'''one two three four
a 0 1 0.0 1.0
b 2 3 nan nan
c 4 5 2.0 3.0
'''print(pd.concat([df1,df2],axis=1,keys=['
level1
','level2
'],names=['
upper
','lower
'],sort=true))
'''upper level1 level2
lower one two three four
a 0 1 0.0 1.0
b 2 3 nan nan
c 4 5 2.0 3.0
'''
DataFrame資料合併
一 join 作用 預設情況下,他是把行索引相同的資料合併到一起 注意 以左為準,沒有的部分用nan補全 例子import pandas as pd import numpy as np df1 pd.dataframe data np.zeros 2,5 index list ab columns...
dataframe的橫向合併
我們在用python處理資料的時候,很多時候會遇到資料合併的問題,我們在這裡介紹dataframe的合併問題,橫向合併我們介紹三種方法 1.1 merge 類似於關係型資料庫的連線方式,可以根據乙個或多個鍵將不同的datframe連線起來。該函式的典型應用場景是,針對同乙個主鍵存在兩張不同欄位的表,...
python合併dataframe物件
每次分析資料過程中,總是會遇到各種問題,一時間想不起來。都說事不過三,這事出了不少次數了,這裡還是記下來,但是可能不那麼全,記下來也僅僅為了以後本人自己需要。這裡宣告如下三個dataframe b np.random.random 3,2 a pd.dataframe b,columns a1 a2...