【问题标题】:Create multiple boolean columns in pandas dataframe based on multiple conditions根据多个条件在熊猫数据框中创建多个布尔列
【发布时间】:2022-01-15 08:11:20
【问题描述】:

我有一个数据集,其中作者按作者顺序(1、2、3 等)排名。

Authorid    Author  Article Articleid   Rank
1            John   article 1   1        1
1            John   article 2   2        2
1            John   article 3   3        3
1            John   article 4   4        3
2            Mary   article 5   5        1
2            Mary   article 6   6        2
2            Mary   article 7   7        1
2            Mary   article 8   8        8

我想再创建三个布尔列If_firstIf_secondIf_last。 这样做的目的 - 我想显示作者在文章中的排名是第 1 位、第 2 位还是最后一位。 last 表示Rank 列中的最大数量(Rank 列中此Authorid 的最大数量)。

If_firstIf_second 可以,这很简单,但不知道如何解决If_last

df.loc[df['Rank'] == 1, 'If_first'] = 1
df.loc[df['Rank'] != 1, 'If_first'] = 0
df.loc[df['Rank'] == 2, 'If_second'] = 1
df.loc[df['Rank'] != 2, 'If_second'] = 0

这里有两条规则

  • If_first = if_last - 把他当作if_first
  • If_second = if_last - 把他当作if_second

预期输出:

Authorid    Author  Article Articleid   Rank    If_first    If_second   If_last
1            John   article 1   1        1       1              0         0
1            John   article 2   2        2       0              1         0
1            John   article 3   3        3       0              0         1 (third is the last here)
2            Mary   article 5   5        1       1              0         0
2            Mary   article 6   6        2       0              1         0
2            Mary   article 7   7        3       0              0         0 (third is not the last here, because of the fourth below, all zeros)
2            Mary   article 8   8        4       0              0         1 (fourth is the last here)

【问题讨论】:

  • 示例数据中是否有多余的行? Authorid 中的最后一个 == 1?它在您的输出中不存在。或者,如果在同一个 Authorid 中有相同排名的行,我们是否必须删除其中一行?
  • @sophocles,我再次更新了输出。如果 1,2,3 作者不是最后一个 - 那么所有值都必须为零。不知何故,我需要计算每个作者 ID 的最大 x 排名并将其分配给最后一个。希望对您有所帮助!

标签: python pandas boolean


【解决方案1】:

试试这个:

df = df.reset_index(drop=True)
res = df.groupby('Authorid')['Rank'].apply(lambda x: [x.idxmin(), x.drop_duplicates()[1:].nsmallest(1).index[0], x.idxmax()])

df[['If_first', 'If_second', 'If_last']] = 0
df.loc[res.str[0].tolist(), 'If_first'] = 1
df.loc[res.str[1].tolist(), 'If_second'] = 1
df.loc[res.str[2].tolist(), 'If_last'] = 1

输出:

>>> df
  Authorid   Author  Article  Articleid  Rank  If_first  If_second  If_last
0     John  article        1          1     1         1          0        0
1     John  article        2          2     2         0          1        0
2     John  article        3          3     3         0          0        1
3     John  article        4          4     3         0          0        0
4     Mary  article        5          5     1         1          0        0
5     Mary  article        6          6     2         0          1        0
6     Mary  article        7          7     1         0          0        0
7     Mary  article        8          8     8         0          0        1

【讨论】:

    【解决方案2】:

    一种方法可能是创建第二个按Articleid 分组的数据框,收集您感兴趣的统计数据:

    df2 = df.groupby('Articleid').agg(mxrank=('Rank', 'max'))
    

    然后通过合并数据框来添加新列:

    dfm = df.merge(df2, how='left', on='Articleid')
    

    带有示例结果(添加了一些行来演示具有多个排名的文章“article4”):

       Authorid Author   Article Articleid Rank mxrank
    0         1   John  article1         1    1      1
    1         1   John  article2         2    2      2
    2         1   John  article3         3    3      3
    3         1   John  article4         4    3      4
    4         1    Foo  article4         4    1      4
    5         1    Bar  article4         4    2      4
    6         1    Baz  article4         4    4      4
    7         2   Mary  article5         5    1      1
    8         2   Mary  article6         6    2      2
    9         2   Mary  article7         7    1      1
    10        2   Mary  article8         8    8      8
    

    然后将mxrank 列与Rank 进行比较以确定每一行的标志。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2021-08-30
      • 2022-01-22
      • 2019-03-27
      • 2019-05-02
      • 2018-02-22
      • 1970-01-01
      相关资源
      最近更新 更多