使用 if 条件 (Pandas) 在不同数据帧上的列之间查找
Lookup between columns on different dataframes with if condition (Pandas)
我有一个结构如下的数据框:
df1 = pd.DataFrame({'animal': ['lab', 'hound', 'terrier', 'retriever', 'sparrow', 'robin', 'blue jay', 'hawk'],
'type': ['dog', 'dog', 'dog', '', 'bird','bird', '', '']})
和其他结构如下的数据框:
df2 = pd.DataFrame({'animal': ['retriever','hawk'],
'type': ['dog','bird']})
我想对 df1 上的 'type' 列进行条件查找,以便输出是 df1 中的一个新列,其中填充了以下逻辑,因此它看起来像这样:
df3 = pd.DataFrame({'animal': ['lab', 'hound', 'terrier', 'retriever', 'sparrow', 'robin', 'blue jay', 'hawk'],
'type': ['dog', 'dog', 'dog', '', 'bird','bird', '', ''],
'type2': ['dog', 'dog', 'dog', 'dog', 'bird','bird', '', 'bird']})
- 如果 df1 中有值['type'] 将此值填充到新列 df1['type2'}
- 如果没有,请查找 df2,如果有匹配值,则将此值填充到 df1['type2]
- 如果没有,则保留 df1['type2'] null
如有任何帮助,我们将不胜感激!
在这里使用地图和 fillna:
df1['type2'] = df1['animal'].map(df2.set_index('animal')['type']).fillna(df1['type'])
或者:
out = df1.merge(df2,on='animal',suffixes=('','_'),how='left')
df1['type2'] = out['type_'].fillna(out['type']).rename('type2')
print(df1)
animal type type2
0 lab dog dog
1 hound dog dog
2 terrier dog dog
3 retriever dog
4 sparrow bird bird
5 robin bird bird
6 blue jay
7 hawk bird
这样做就可以了:
import numpy as np
# build lookup table for all animals from df1, based on df2:
df2 = df2.set_index("animal").reindex(df1["animal"].unique()).fillna("")
df1["type2"] = np.where(df1["type"].str.len()>0, df1["type"], df2["type"].loc[df1["animal"]].reset_index(drop=True))
输出:
animal type type2
0 lab dog dog
1 hound dog dog
2 terrier dog dog
3 retriever dog
4 sparrow bird bird
5 robin bird bird
6 blue jay
7 hawk bird
我有一个结构如下的数据框:
df1 = pd.DataFrame({'animal': ['lab', 'hound', 'terrier', 'retriever', 'sparrow', 'robin', 'blue jay', 'hawk'],
'type': ['dog', 'dog', 'dog', '', 'bird','bird', '', '']})
和其他结构如下的数据框:
df2 = pd.DataFrame({'animal': ['retriever','hawk'],
'type': ['dog','bird']})
我想对 df1 上的 'type' 列进行条件查找,以便输出是 df1 中的一个新列,其中填充了以下逻辑,因此它看起来像这样:
df3 = pd.DataFrame({'animal': ['lab', 'hound', 'terrier', 'retriever', 'sparrow', 'robin', 'blue jay', 'hawk'],
'type': ['dog', 'dog', 'dog', '', 'bird','bird', '', ''],
'type2': ['dog', 'dog', 'dog', 'dog', 'bird','bird', '', 'bird']})
- 如果 df1 中有值['type'] 将此值填充到新列 df1['type2'}
- 如果没有,请查找 df2,如果有匹配值,则将此值填充到 df1['type2]
- 如果没有,则保留 df1['type2'] null
如有任何帮助,我们将不胜感激!
在这里使用地图和 fillna:
df1['type2'] = df1['animal'].map(df2.set_index('animal')['type']).fillna(df1['type'])
或者:
out = df1.merge(df2,on='animal',suffixes=('','_'),how='left')
df1['type2'] = out['type_'].fillna(out['type']).rename('type2')
print(df1)
animal type type2
0 lab dog dog
1 hound dog dog
2 terrier dog dog
3 retriever dog
4 sparrow bird bird
5 robin bird bird
6 blue jay
7 hawk bird
这样做就可以了:
import numpy as np
# build lookup table for all animals from df1, based on df2:
df2 = df2.set_index("animal").reindex(df1["animal"].unique()).fillna("")
df1["type2"] = np.where(df1["type"].str.len()>0, df1["type"], df2["type"].loc[df1["animal"]].reset_index(drop=True))
输出:
animal type type2
0 lab dog dog
1 hound dog dog
2 terrier dog dog
3 retriever dog
4 sparrow bird bird
5 robin bird bird
6 blue jay
7 hawk bird