迭代行并加入python pandas

时间:2018-06-05 17:49:04

标签: python pandas merge

我有像这样的主数据集

master = pd.DataFrame({'Channel':['1','1','1','1','1'],'Country':['India','Singapore','Japan','United Kingdom','Austria'],'Product':['X','6','7','X','X']})

master

和像这样的用户表

user = pd.DataFrame({'User':['101','101','102','102','102','103','103','103','103','103'],'Country':['India','Brazil','India','Brazil','Japan','All','Austria','Japan','Singapore','United Kingdom'],'count':['2','1','3','2','1','1','1','1','1','1']})

user

我希望主表左连接与每个用户的用户表。如下一个用户

   merge_101 = pd.merge(master,user[(user.User=='101')],how='left',on=['Country'])
   merge_102 = pd.merge(master,user[(user.User=='102')],how='left',on=['Country'])
   merge_103 = pd.merge(master,user[(user.User=='103')],how='left',on=['Country'])
   merge_all = pd.concat([merge_101, merge_102,merge_103], ignore_index=True)

如何在这里迭代每个用户我首先过滤数据集并创建另一个数据集并稍后附加整个数据集。

有没有更好的方法来执行此任务,例如for循环或任何连接?

由于

1 个答案:

答案 0 :(得分:0)

IIUC,您需要:

pd.concat([pd.merge(master,user[(user.User==x)],how='left',on=['Country']) for x in list(user['User'].unique())], ignore_index=True)

输出:

    Channel         Country Product User count
0        1           India       X  101     2
1        1       Singapore       6  NaN   NaN
2        1           Japan       7  NaN   NaN
3        1  United Kingdom       X  NaN   NaN
4        1         Austria       X  NaN   NaN
5        1           India       X  102     3
6        1       Singapore       6  NaN   NaN
7        1           Japan       7  102     1
8        1  United Kingdom       X  NaN   NaN
9        1         Austria       X  NaN   NaN
10       1           India       X  NaN   NaN
11       1       Singapore       6  103     1
12       1           Japan       7  103     1
13       1  United Kingdom       X  103     1
14       1         Austria       X  103     1