简体   繁体   English

基于另一个 dataframe 列在 dataframe 中添加新列

[英]adding a new column in a dataframe based on another dataframe column

Let's assume we have the following 2 dataframes:假设我们有以下 2 个数据帧:

df1(36000, 20) and  df2(80,6)
They have 3 columns in common(let's say Name, Last Name, Date)

df1 includes the data of df2 (minus the data in the 3 different columns) and of course some extra information. df1 包括 df2 的数据(减去 3 个不同列中的数据),当然还有一些额外的信息。
df2 has a column that I am interested (let' s name it Rent) df2 有一个我感兴趣的栏目(我们将其命名为 Rent)

What I want is to create an extra column in df1 that for the values that of df2 to have the value "Overdue" and for the values that are not there have "Due" while keeping the rest of columns in df1.我想要的是在 df1 中创建一个额外的列,其中 df2 的值具有“过期”值,而不存在的值具有“到期”,同时保持 df1 中列的 rest。 I tried the following我尝试了以下

  merged = df1.merge(df2, how='left', on=list(df1.columns),
                                   indicator=True)
    df1['Rent'] = np.where(merged['_merge'] == 'both', 'Overdue', 'Due')

However I get an error due to the fact that not all columns of df1 exist in df2.但是,由于并非 df1 的所有列都存在于 df2 中,因此出现错误。 Any ideas?有任何想法吗?

Also I tried the following我也尝试了以下

    df1['Rent'].apply(lambda x: 'Overdue' if df1['Name'].isin(df2['Name']) else 'Due')

but I m getting the following error但我收到以下错误

AttributeError: 'function' object has no attribute 'df2'

Try this:尝试这个:

df1['Rent'] = lambda x: 'Overdue' if df1['Name'].isin(df2['Name']) else 'Due'

The main point is not to use.apply()重点是不要使用.apply()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM