pandas - 从multindex列获取值

时间:2015-07-14 10:44:04

标签: python pandas

我有以下数据框df:

H,Nu,City,Code,Code2
0.965392,15,Madrid,es,es
0.920614,15,Madrid,it,es
0.726219,16,Madrid,tn,es
0.739119,17,Madrid,fr,es
0.789923,55,Dublin,mt,en
0.699239,57,Dublin,en,en
0.890462,68,Dublin,ar,en
0.746863,68,Dublin,pt,en
0.789923,55,Milano,it,it
0.699239,57,Milano,es,it
0.890462,68,Milano,ar,it
0.746863,68,Milano,pt,it

我想为每个HCode添加一个新列City,其中H值对应Code字符串映射的Code2 ,以便生成的数据框显示为:

H,Nu,City,Code,Code2,HCode
0.965392,15,Madrid,es,es,0.965392
0.920614,15,Madrid,it,es,0.965392
0.726219,16,Madrid,tn,es,0.965392
0.739119,17,Madrid,fr,es,0.965392
0.789923,55,Dublin,mt,en,0.699239
0.699239,57,Dublin,en,en,0.699239
0.890462,68,Dublin,ar,en,0.699239
0.746863,68,Dublin,pt,en,0.699239
0.789923,55,Milano,it,it,0.789923
0.699239,57,Milano,es,it,0.789923
0.890462,68,Milano,ar,it,0.789923
0.746863,68,Milano,pt,it,0.789923

到目前为止,我尝试按城市和Code2分组,但没有结果。

1 个答案:

答案 0 :(得分:1)

您可以groupby点击城市'和' Code2',对此调用first并重置索引,结果如下:

In [172]:
gp = df.groupby(['City','Code2'])['H'].first().reset_index()
gp

Out[172]:
     City Code2         H
0  Dublin    en  0.789923
1  Madrid    es  0.965392
2  Milano    it  0.789923

然后在原始df上执行左合并并选择' H_y'列,该名称来自列冲突和ffill这个:

的事实
In [173]:
df['HCode'] = df.merge(gp, left_on=['City', 'Code'], right_on=['City', 'Code2'], how='left')['H_y'].ffill()
df

Out[173]:
           H  Nu    City Code Code2     HCode
0   0.965392  15  Madrid   es    es  0.965392
1   0.920614  15  Madrid   it    es  0.965392
2   0.726219  16  Madrid   tn    es  0.965392
3   0.739119  17  Madrid   fr    es  0.965392
4   0.789923  55  Dublin   mt    en  0.965392
5   0.699239  57  Dublin   en    en  0.789923
6   0.890462  68  Dublin   ar    en  0.789923
7   0.746863  68  Dublin   pt    en  0.789923
8   0.789923  55  Milano   it    it  0.789923
9   0.699239  57  Milano   es    it  0.789923
10  0.890462  68  Milano   ar    it  0.789923
11  0.746863  68  Milano   pt    it  0.789923

merge显示其产生结果的结果:

In [165]:
df.merge(gp, left_on=['City', 'Code'], right_on=['City', 'Code2'])['H_y']

Out[165]:
0    0.965392
1    0.789923
2    0.789923
Name: H_y, dtype: float64

修改

好的,IIUC你可以像以前一样分组但是然后过滤那个' Code2'等于'代码'然后使用它来合并:

In [200]:
gp = df.groupby('City')
mask = gp.apply(lambda x: x['Code2'] == x['Code'])
lookup = df.loc[mask[mask].reset_index(level=0).index]
lookup

Out[200]:
          H  Nu    City Code Code2
5  0.699239  57  Dublin   en    en
0  0.965392  15  Madrid   es    es
8  0.789923  55  Milano   it    it

In [202]:
df['HCode'] = df.merge(lookup, left_on=['City', 'Code'], right_on=['City', 'Code2'], how='left')['H_y'].ffill()
df

Out[202]:
           H  Nu    City Code Code2     HCode
0   0.965392  15  Madrid   es    es  0.965392
1   0.920614  15  Madrid   it    es  0.965392
2   0.726219  16  Madrid   tn    es  0.965392
3   0.739119  17  Madrid   fr    es  0.965392
4   0.789923  55  Dublin   mt    en  0.965392
5   0.699239  57  Dublin   en    en  0.699239
6   0.890462  68  Dublin   ar    en  0.699239
7   0.746863  68  Dublin   pt    en  0.699239
8   0.789923  55  Milano   it    it  0.789923
9   0.699239  57  Milano   es    it  0.789923
10  0.890462  68  Milano   ar    it  0.789923
11  0.746863  68  Milano   pt    it  0.789923