简体   繁体   English

如何将列添加到 dataframe 中,其值取决于另一个 dataframe?

[英]How can I add a column to a dataframe with a value conditional on another dataframe?

I'm working with two dataframes:我正在使用两个数据框:

Dataframe1 looks like: Dataframe1 看起来像:

user (index)用户(索引) apples苹果 bananas香蕉
Pete皮特 4 4 2 2
Sara萨拉 5 5 10 10
Kara卡拉 4 4 2 2
Tom汤姆 3 3 3 3

Dataframe2 looks like: Dataframe2 看起来像:

index指数 user用户
1 1 Pete皮特
2 2 Sara萨拉

I want to create a new boolean column in dataframe1 that is true if the user is in dataframe 2. So output looks like:我想在 dataframe1 中创建一个新的 boolean 列,如果用户在 dataframe 2 中,这是真的。所以 output 看起来像:

user用户 apples苹果 bananas香蕉 new column新专栏
Pete皮特 4 4 2 2 True真的
Sara萨拉 5 5 10 10 True真的
Kara卡拉 4 4 2 2 False错误的
Tom汤姆 3 3 3 3 False错误的

I tried using lambda function but didn't get very far.我尝试使用 lambda function 但没有走得很远。

Here is an easy way of doing that.这是一个简单的方法。

df = df.reset_index()
df2['new_column']=True

df = pd.merge(df, df2, left_on='user', right_on='user', how = 'left')
df.new_column.fillna(False, inplace=True)

You can leverage the indicator param of df.merge .您可以利用df.mergeindicator参数。 Then use df.replace :然后使用df.replace

In [598]: x = df1.merge(df2['user'], left_on='user (index)', right_on='user', how='left', indicator='new column').replace({'both': True, 'left_only':False}).drop('user', 1)

In [599]: x
Out[599]: 
  user (index)  apples  bananas  new column
0         Pete       4        2        True
1         Sara       5       10        True
2         Kara       4        2       False
3          Tom       3        3       False

OR:或者:

For some better performance, use Series.map instead of df.replace :为了获得更好的性能,请使用Series.map而不是df.replace

In [609]: y = df1.merge(df2['user'], left_on='user (index)', right_on='user', how='left', indicator='new column').drop('user', 1)

In [611]: y['new column'] = y['new column'].map({'both': True, 'left_only':False})

In [612]: y
Out[612]: 
  user (index)  apples  bananas new column
0         Pete       4        2       True
1         Sara       5       10       True
2         Kara       4        2      False
3          Tom       3        3      False

暂无
暂无

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

相关问题 我如何根据另一个具有不同长度但共享列数据的 dataframe 的条件向 dataframe 添加一列 - How can i add a column to a dataframe based on a conditional of another dataframe that has a different length, but shared column data 如何将一列添加到基于另一个列值的数据框中? - How can I add a column to a dataframe that is based on another columns value? 如何将一列从一个数据帧添加到另一个数据帧? - How can I add a column from one dataframe to another dataframe? 如何在另一个数据帧 python pandas 中的多列上使用条件逻辑在数据帧中创建一列? - How can I create a column in a dataframe using conditional logic on multiple columns in another dataframe python pandas? 如果值是 integer,如何在 Dataframe 列中添加前导零,以另一列的值为条件? - How to add leading zeroes to Dataframe column if value is an integer, conditional on another column's values? 我如何使用另一个数据帧列过滤数据帧,它们在第二个数据帧中都有不同的索引和很少的值? - How i can filter a dataframe with another dataframe column, both have different index and few value in second dataframe? 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中? - How do I add the value from one dataframe based on a column to another dataframe based on a row? 如果值在另一个 dataframe 的另一列中,则添加列 - Add column if value is in another column of another dataframe 如何将 pandas dataframe 中的列值作为新的键值对添加到具有字典的另一列中? - How can I add column values from a pandas dataframe as a new key value pair in another column having dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM