使用 join 在 pandas 中加入 2 个数据帧
Join 2 dataframes in pandas using join
我有两个数据框 df1 和 df2
df1
id name
ada1 mike
ad23 tom
cev2 tim
df2
id month. sales
ada1. 1/11. 23
ada1. 4/11. 34
ad23. 3/12. 34
cev2. 4/11. 32
我需要:
id month. sales name
ada1. 1/11. 23. mike
ada1. 4/11. 34. mike
ad23. 3/12. 34. tom
cev2. 4/11. 32. tim
我在左连接和右连接之间纠结,我应该用什么。
使用pd.merge
:
>>> df2.merge(df1, on='id', how='left')
id month sales name
0 ada1 1/11 23 mike
1 ada1 4/11 34 mike
2 ad23 3/12 34 tom
3 cev2 4/11 32 tim
使用地图,因为您在一列上 joining\merging 并返回一列。
df2['name'] = df2['id'].map(df1.set_index('id')['name'])
Map 将胜过 join\merge。
根据问题集提供两种可以提供帮助的方法
import pandas as pd
# retain df2 and merge df1 use 'leftjoin'
df2.merge(df1, on='id', how='left')
# Full Join, also known as Full Outer Join, returns all those records which
either have a match in the left or right dataframe
pd.merge(df2,df1,on='id',how='outer',indicator=True)
我有两个数据框 df1 和 df2
df1
id name
ada1 mike
ad23 tom
cev2 tim
df2
id month. sales
ada1. 1/11. 23
ada1. 4/11. 34
ad23. 3/12. 34
cev2. 4/11. 32
我需要:
id month. sales name
ada1. 1/11. 23. mike
ada1. 4/11. 34. mike
ad23. 3/12. 34. tom
cev2. 4/11. 32. tim
我在左连接和右连接之间纠结,我应该用什么。
使用pd.merge
:
>>> df2.merge(df1, on='id', how='left')
id month sales name
0 ada1 1/11 23 mike
1 ada1 4/11 34 mike
2 ad23 3/12 34 tom
3 cev2 4/11 32 tim
使用地图,因为您在一列上 joining\merging 并返回一列。
df2['name'] = df2['id'].map(df1.set_index('id')['name'])
Map 将胜过 join\merge。
根据问题集提供两种可以提供帮助的方法
import pandas as pd
# retain df2 and merge df1 use 'leftjoin'
df2.merge(df1, on='id', how='left')
# Full Join, also known as Full Outer Join, returns all those records which
either have a match in the left or right dataframe
pd.merge(df2,df1,on='id',how='outer',indicator=True)