pandas在合并两个数据帧时创建一个标志

时间:2018-06-14 10:16:22

标签: python-3.x pandas dataframe merge

我有两个df - df_adf_b

# df_a
number    cur
1000      USD
2000      USD
3000      USD

# df_b
number    amount    deletion
1000      0.0       L
1000      10.0      X
1000      10.0      X
2000      20.0      X
2000      20.0      X
3000      0.0       L
3000      0.0       L

我想将df_adf_b合并,

df_a = df_a.merge(df_b.loc[df_b.deletion != 'L'], how='left', on='number')
df_a.fillna(value={'amount':0}, inplace=True)

还要在结果deleted中创建一个名为df_a的标记,该标记有三个可能的值 - fullpartialnone;

full - 如果与特定number值相关联的所有行都有deletion = L;

partial - 如果某些行与特定number值相关联,则deletion = L;

none - 没有与特定number值相关联的行,deletion = L;

同样在进行合并时,不应考虑来自df_bdeletion = L的行;所以结果看起来像,

 number    amount    deletion    deleted    cur
 1000      10.0      X           partial    USD
 1000      10.0      X           partial    USD
 2000      20.0      X           none       USD
 2000      20.0      X           none       USD
 3000      0.0       NaN         full       USD

我想知道如何实现这一目标。

1 个答案:

答案 0 :(得分:1)

理念是比较deletion列和汇总allany,为新列创建助手dictionary和最后map

g = df_b['deletion'].eq('L').groupby(df_b['number'])
m1 = g.any()
m2 = g.all()

d1 = dict.fromkeys(m1.index[m1 & ~m2], 'partial')
d2 = dict.fromkeys(m2.index[m2], 'full')
#join dictionries together
d = {**d1, **d2}
print (d)
{1000: 'partial', 3000: 'full'}

df = df_a.merge(df_b.loc[df_b.deletion != 'L'], how='left', on='number')
df['deleted'] = df['number'].map(d).fillna('none')
print (df)
   number  cur  amount deletion  deleted
0    1000  USD    10.0        X  partial
1    1000  USD    10.0        X  partial
2    2000  USD    20.0        X     none
3    2000  USD    20.0        X     none
4    3000  USD     NaN      NaN     full

对于指定列none,如果要为其创建字典:

d1 = dict.fromkeys(m1.index[m1 & ~m2], 'partial')
d2 = dict.fromkeys(m2.index[m2], 'full')
d3 = dict.fromkeys(m2.index[~m1], 'none')
d = {**d1, **d2, **d3}
print (d)
{1000: 'partial', 3000: 'full', 2000: 'none'}

df = df_a.merge(df_b.loc[df_b.deletion != 'L'], how='left', on='number')
df['deleted'] = df['number'].map(d)
print (df)
   number  cur  amount deletion  deleted
0    1000  USD    10.0        X  partial
1    1000  USD    10.0        X  partial
2    2000  USD    20.0        X     none
3    2000  USD    20.0        X     none
4    3000  USD     NaN      NaN     full