简体   繁体   English

如果值相等,则将列值从 dataframe 复制到另一个

[英]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 中,您的idstring类型,
and in the other dataframe, the id is of type int .在另一个 dataframe 中, idint类型。

The easiest way to resolve this is to convert the string to int before merge ,解决这个问题的最简单方法是在 merge 之前将字符串转换为 int ,
and convert back if so desired.并在需要时转换回来。

Code:代码:

df_p.id = df_p.id.astype(int)
df_c = pd.merge(df_c, df_p, on=['id'], how='left')

print(df_c)

Output: Output:

df_c

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将值从一个 dataframe 列复制到另一列 - Copy values from one dataframe column to another 如果值等于某个数字,则将值从一列复制到另一列 - Copy values from one column to another if values equal to a certain number 创建一个新的 pandas dataframe 列,其中包含一个附加值,然后是其下方另一列的值的副本 - Creating a new pandas dataframe column with one added value, then a copy of values from another column underneath it 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas 从一列中删除等于另一列中的值的值 - Remove values from one column, that are equal to the value in another 根据 Pandas 中的 id 将列值从一个数据帧复制到另一个数据帧 - Copy column value from one dataframe to another based on id in Pandas 根据多列索引将值从一个 dataframe 复制到另一个 - Copy value from one dataframe to another based on multiple column index Pandas:将从 DataFrame 中提取的值乘以另一个 DataFrame 中的列值 - Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame 将过滤后的数据帧行的值设置为等于另一个数据帧的列 - Setting the values of filtered rows of dataframe equal to the column of another dataframe 根据索引使Pandas Dataframe列等于另一个Dataframe中的值 - Make Pandas Dataframe column equal to value in another Dataframe based on index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM