一 前言
pandas数据拼接有可能会用到,比如出现重复数据,需要合并两份数据的交集,并集就是个不错的选择,知识追寻者本着技多不压身的态度蛮学习了一下下;
二 数据拼接
在进行学习数据转换之前,先学习一些数拼接相关的知识
2.1 join()联结
有关merge操作知识追寻者这边不提及,有空可能后面会专门出一篇相关文章,因为其学习方式根SQL的表联结类似,不是几行能说清楚的知识点;
join操作能将 2 个DataFrame 合并为一块,前提是DataFrame 之间的列没有重复;
# -*- coding: utf-8 -*- import pandas as pd import numpy as np data1 = { 'user' : ['zszxz','craler','rose'], 'price' : [100, 200, 300], 'hobby' : ['reading','running','hiking'] } index1 = ['user1','user2','user3'] frame1 = pd.DataFrame(data1,index1) data2 = { 'person' : ['zszxz','craler','rose'], 'number' : [100, 2000, 3000], 'activity' : ['swing','riding','climbing'] } 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
2.2 concat()拼接
使用 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 类似;
2.3 combine_first()组合
索引重复时就可以使用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
2.4 轴转换
准备的数据
# -*- coding: utf-8 -*- import pandas as pd import numpy as np data = { 'user' : ['zszxz','craler','rose'], 'price' : [100, 200, 300], 'hobby' : ['reading','running','hiking'] } 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
stack() 将 列转为行;
# -*- coding: utf-8 -*- import pandas as pd import numpy as np data = { 'user' : ['zszxz','craler','rose'], 'price' : [100, 200, 300], 'hobby' : ['reading','running','hiking'] } 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
使用 unstack()将 数据结构重新返回
# -*- coding: utf-8 -*- import pandas as pd import numpy as np data = { 'user' : ['zszxz','craler','rose'], 'price' : [100, 200, 300], 'hobby' : ['reading','running','hiking'] } 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,数据拼接
更新日志
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】