[英]Copy column value from a dataframe to another if values are equal
I have two dataframes like that (this is an example because my dataframes are complex):我有两个这样的数据框(这是一个例子,因为我的数据框很复杂):
lst_p = [['2', 0], ['3', 1], ['4', 0], ['5', 0]]
df_p = pd.DataFrame(lst_p, columns =['id', 'redness'])
lst_c = [['apple', 2], ['orange', 2], ['banana', 3], ['kiwi', 4], ['cherry', 5]]
df_c = `pd.DataFrame(lst_c, columns =['name', 'id'])`
My two dataframes don't have the same length.我的两个数据帧的长度不同。
As you can see in my second df_c, some 'id' appears 2 times.正如您在我的第二个 df_c 中所见,某些“id”出现了 2 次。 (for id=2)
(对于 id=2)
I would like to create a new column in my df_c
that copy the value 'redness' of my df_p
if 'id'
from my df_c == 'id' from my df_p
.我想在我的
df_c
中创建一个新列,如果'id'
来自我的 df_c == 'id' 从我的df_p
复制我的 df_p 的值'redness' of my df_p
' 。
I don't know if it's very clear...不知道说的清楚不。。。
Thanks a LOT !!!多谢 !!!
Use can simply try to convert df_p two column to dictionary any using lambda look for each id's redness, and create new column.使用可以简单地尝试将 df_p 两列转换为字典 any 使用 lambda 查找每个 id 的红色,并创建新列。
Code:代码:
df_c['redness'] = df_c['id'].apply(lambda x: pd.Series(df_p.redness.values,index=df_p.id).to_dict()[str(x)])
df_c
A simple merge will do the trick:一个简单的合并就可以解决问题:
One issue that you have is that in one dataframe your id
is of type string
,您遇到的一个问题是,在一个 dataframe 中,您的
id
是string
类型,
and in the other dataframe, the id
is of type int
.在另一个 dataframe 中,
id
是int
类型。
The easiest way to resolve this is to convert the string to int before merge ,解决这个问题的最简单方法是在 merge 之前将字符串转换为 int ,
and convert back if so desired.并在需要时转换回来。
df_p.id = df_p.id.astype(int)
df_c = pd.merge(df_c, df_p, on=['id'], how='left')
print(df_c)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.