【问题标题】:Add output column based on conditions from another dataset根据来自另一个数据集的条件添加输出列
【发布时间】:2020-05-05 03:45:03
【问题描述】:
job Education   Age   Number of relatives   
1   1            25          5  
1   2            23          20 
3   4            26          50 
2   1            37          100    
4   3            29          34 

output    Job   Education   agemin  agemax  relativesmin    relativesmax
Category1   1   1            25      34       1                 11
Category2   2   3            35      44       11                50
Category3   3   2            45      100      50                200

所以问题是如何在第一个数据集中添加列输出,但基于条件(df1.job == df2.Job ... 并且年龄在第二个数据集的 agemin 和 agemax 之间)输出应该如下所示:

job Education   Age   Number of relatives    output 
1   1            25          5                Category1
1   2            23          20               Category2
3   4            26          50               Uncategorized
2   1            37          100              ....
4   3            29          34               ....

我尝试了几种方法,包括 iterrows 并加入两个数据集,但我没有得到我需要的结果

【问题讨论】:

  • 你知道merge_asof吗?这在这种情况下应该会有所帮助

标签: python-3.x pandas dataset


【解决方案1】:

IIUC,

我们可以合并然后使用带有列分配的简单过滤器:

df2.columns = df2.columns.str.lower()
df_new = pd.merge(df1, df2[["job", "agemin", "agemax", "output"]], on="job", how="left")

df_new.loc[
    ~((df_new["Age"] >= df_new["agemin"]) & (df_new["Age"] <= df_new["agemax"])), "output"
] = "Uncategorised"

print(df_new)

   job  Education  Age  Number_of_relatives  agemin  agemax         output
0    1          1   25                    5    25.0    34.0      Category1
1    1          2   23                   20    25.0    34.0  Uncategorised
2    3          4   26                   50    45.0   100.0  Uncategorised
3    2          1   37                  100    35.0    44.0      Category2
4    4          3   29                   34     NaN     NaN            NaN

【讨论】:

  • job Educ Age N_rel amin amax output 2 3 46 90 35.0 44.0 此行中的 Category2 年龄不在 35 到 44 之间,但我仍然得到 category2,这是我发现的唯一问题
  • df_new.loc[~((df_new["Age"] >= df_new["amin"]) & (df_new["Age"]
【解决方案2】:

这是一种使用IntervalIndex.from_arraysreindexassign 的方法:

s = pd.IntervalIndex.from_arrays(df2['agemin'],df2['agemax'],'left')
d = df2.set_index(s).reindex(df1['Age']).loc[:,['output','Job']]
         .groupby(level=0,sort=False).first().set_index('Job',append=True))
final = (df1.set_index(['Age','job']).assign(**d)
           .fillna({'output':'Uncategorized'}).reset_index())

print(final)

   Age  job  Education  Number_of_relatives         output
0   25    1          1                    5      Category1
1   23    1          2                   20  Uncategorized
2   26    3          4                   50  Uncategorized
3   37    2          1                  100      Category2
4   29    4          3                   34  Uncategorized

【讨论】:

  • ir 提出了一个无法处理非唯一多索引的问题!错误
  • @AidouniIsmail 对,我用一个测试用例进行了一些编辑。现在应该可以工作了,但对于这个用例 Datanovice 的答案更具可读性
猜你喜欢
  • 1970-01-01
  • 2013-06-14
  • 2017-11-10
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 2017-10-12
相关资源
最近更新 更多