【问题标题】:How to check if data frame column value appears in all unique year in the data frame column如何检查数据框列值是否出现在数据框列的所有唯一年份中
【发布时间】:2021-05-09 13:03:40
【问题描述】:
df1 = pd.DataFrame({'type': ['cst1', 'cst1', 'cst1','cst1','cst2','cst2','cst2','cst3','cst3','cst3','cst3'],'year':[2017,2018,2019,2020,2018,2019,2020,2017,2018,2019,2020]})

   type  year
0   cst1  2017
1   cst1  2018
2   cst1  2019
3   cst1  2020
4   cst2  2018
5   cst2  2019
6   cst2  2020
7   cst3  2017
8   cst3  2018
9   cst3  2019
10  cst3  2020

对于上述数据框,需要检查每个类型值是否存在于所有四年中 [2017,2018,2019,2020] 需要标记为 1,否则标记为 0。 例如:第一种类型 cst1 出现在所有 4 年中,因此标记为 1,cst2 仅出现在 3 年中,因此标记为 1。 注意:理想情况下,仅包含四年,即 2017 - 2020。类型和年份组合将是唯一的。

想要的输出:

type  year label
0   cst1  2017     1
1   cst1  2018     1
2   cst1  2019     1
3   cst1  2020     1
4   cst2  2018     0
5   cst2  2019     0
6   cst2  2020     0
7   cst3  2017     1
8   cst3  2018     1
9   cst3  2019     1
10  cst3  2020     1

【问题讨论】:

  • 如果有[2016,2017,2018,2019,2020,2021]cst4 会是 1 还是 0?
  • 如果cst4[2017, 2017, 2018, 2019, 2020, 2020],其他问题会是1 还是0?
  • @HenryEcker 是的,好点子!让我们也将[2019,2017,2018,2020] 添加到我们的列表中...... :)
  • @perl 感谢您的回复,数据仅包含从 2017 年到 2020 年的四年。类型和年份组合将是唯一的。

标签: python pandas


【解决方案1】:

我猜如果所有年份都在 2017 -2020 年范围内,那么使用 nunique 进行 groupby/transform 就可以了:

df['label'] = (df1.groupby('type').transform('nunique') == 4).astype(int)

替代方案:

df1['label'] = 0 
def test(x):
    return set(x.values) == {2017,2018,2019,2020}
df1.iloc[df1.groupby('type')['year'].filter(test).index , 2] = 1

【讨论】:

  • 比较确切的年份而不是年数会更好,因为如果有 4 年但不需要 4 年,它将失败。
【解决方案2】:
  • 使用groupby()根据类型创建组
  • 使用transform()获取基于组的每一行中的年元组
  • 将这些元组与您所需的年份进行比较。每行都会产生 True/False
  • 使用 astype('int') 将布尔值 (True/False) 转换为整数 (1/0)
required = (2017,2018,2019,2020)
df1["label"] = (df1.groupby('type').transform(tuple)["year"] == required).astype('int')

print(df1)

    type    year    label
0   cst1    2017    1
1   cst1    2018    1
2   cst1    2019    1
3   cst1    2020    1
4   cst2    2018    0
5   cst2    2019    0
6   cst2    2020    0
7   cst3    2017    1
8   cst3    2018    1
9   cst3    2019    1
10  cst3    2020    1

【讨论】:

    【解决方案3】:

    让我们试试吧:

    1. groupby transform 测试每个组的年份是否是所需年份的子集。
    2. 使用 astype(int) 将布尔值转换为 1 和 0
    import pandas as pd
    
    df1 = pd.DataFrame({'type': ['cst1', 'cst1', 'cst1', 'cst1', 'cst2', 'cst2',
                                 'cst2', 'cst3', 'cst3', 'cst3', 'cst3'],
                        'year': [2017, 2018, 2019, 2020, 2018, 2019, 2020, 2017,
                                 2018, 2019, 2020]})
    
    years = {2017, 2018, 2019, 2020}
    
    df1['label'] = (
        df1.groupby('type').year.transform(lambda x: years.issubset(x))
    ).astype(int)
    print(df1)
    

    df1:

        type  year  label
    0   cst1  2017      1
    1   cst1  2018      1
    2   cst1  2019      1
    3   cst1  2020      1
    4   cst2  2018      0
    5   cst2  2019      0
    6   cst2  2020      0
    7   cst3  2017      1
    8   cst3  2018      1
    9   cst3  2019      1
    10  cst3  2020      1
    

    *请注意,这将匹配具有至少四年的任何组。因此,如果一个组包含来自 [2016, 2017, 2018, 2019, 2020] 的条目,它将被匹配。

    【讨论】:

      猜你喜欢
      • 2019-06-21
      • 2019-11-22
      • 1970-01-01
      • 2021-06-26
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多