pandas資料拼接有可能會用到,比如出現重複資料,需要合併兩份資料的交集,並集就是個不錯的選擇,知識追尋者本著技多不壓身的態度蠻學習了一下下;
知識追尋者(inheriting the spirit of open source, spreading technology knowledge;)在進行學習資料轉換之前,先學習一些數拼接相關的知識
join操作能將 2 個dataframe 合併為一塊,前提是dataframe 之間的列沒有重複;
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
data1 =
index1 = ['user1','user2','user3']
frame1 = pd.dataframe(data1,index1)
data2 =
index2 = ['user1','user2','user3']
frame2 = pd.dataframe(data2,index2)
join = frame1.join(frame2)
print(join)
輸出
user price hobby person number activity
user1 zszxz 100 reading zszxz 100 swing
user2 craler 200 running craler 2000 riding
user3 rose 300 hiking rose 3000 climbing
使用concat()
函式能將2個 series 拼接為乙個,預設按行拼接;
ser1 = pd.series(['111','222',np.nan])
ser2 = pd.series(['333','444',np.nan])
# 預設按行拼接
print(pd.concat([ser1, ser2]))
如果按列拼接則 axis = 1
ser1 = pd.series(['111','222',np.nan])
ser2 = pd.series(['333','444',np.nan])
# 按列拼接
print(pd.concat([ser1, ser2],axis=1))
輸出
0 1
0 111 333
1 222 444
2 nan nan
更近一步,指定key 引數 輸出的資料格式就和 dataframe 一樣
ser1 = pd.series(['111','222',np.nan])
ser2 = pd.series(['333','444',np.nan])
# 按列拼接
data = pd.concat([ser1, ser2],axis=1, keys=['zszxz', 'rzxx'])
print(data)
輸出
zszxz rzxx
0 111 333
1 222 444
2 nan nan
注 : dataframe 的 concat 操作 和 series 類似;
索引重複時就可以使用combine_first
進行拼接
ser1 = pd.series(['111','222',np.nan],index=[1,2,3])
ser2 = pd.series(['333','444',np.nan,'555'],index=[1,2,3,4])
data = ser1.combine_first(ser2)
print(data)
輸出
1 111
2 222
3 nan
4 555
dtype: object
將series 位置互換一下,可以看見基準將以 ser2為準;
ser1 = pd.series(['111','222',np.nan],index=[1,2,3])
ser2 = pd.series(['333','444',np.nan,'555'],index=[1,2,3,4])
data = ser2.combine_first(ser1)
print(data)
輸出
1 333
2 444
3 nan
4 555
dtype: object
準備的資料
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
data =
index = ['user1','user2','user3']
frame = pd.dataframe(data,index)
print(frame)
輸出
user price hobby
user1 zszxz 100 reading
user2 craler 200 running
user3 rose 300 hiking
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
data =
index = ['user1','user2','user3']
frame = pd.dataframe(data,index)
print(frame.stack())
輸出
user1 user zszxz
price 100
hobby reading
user2 user craler
price 200
hobby running
user3 user rose
price 300
hobby hiking
dtype: object
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
data =
index = ['user1','user2','user3']
frame = pd.dataframe(data,index)
sta = frame.stack()
print(sta.unstack())
輸出
user price hobby
user1 zszxz 100 reading
user2 craler 200 running
user3 rose 300 hiking
pandas資料拼接的實現示例
一 前言 pandas資料拼接有可能會用到,比如出現重複資料,需要合併兩份資料的交集,並集就是個不錯的選擇,知識追尋者本著技多不壓身的態度蠻學習了一下下 二 資料拼接 在進行學習資料轉換之前,先學習一些數拼接相關的知識 2.1 join 聯結 有關merge操作知識追尋者這邊不提及,有空可能後面會專...
Pandas的拼接操作
import numpy as np from pandas import dataframe,series import pandas as pd 0回顧numpy的級聯 練習 1.生成2個3 3的矩陣,對其分別進行兩個維度上的級聯 nd np.random.randint 0,10,size 3...
pandas的拼接操作
pandas的拼接分為兩種 合併 pd.merge def make df index,cols df pd.dataframe df.index index return df df1 make df 1 2,3 4,5 6 list abcdef df2 df1.copy display df1...