【问题标题】:Apply recode pattern to many columns将重新编码模式应用于多列
【发布时间】:2021-06-29 06:27:23
【问题描述】:

我有一个包含以下列的数据框:

Name, Year, V1, V2, V5, V10, V12...

此表包含大约 40 个 Vx 变量。这些变量的值可以是 1-5。我想重新编码它们

1-3 = 0 and
4-5 = 1

我知道如何像这样替换一列的数据

Table['V1_F'] = Table['V1'].apply(lambda x: 0 if x <4 else 1)

但我不知道如何有效地将其应用于多列,或者现在有办法为每一列编写此替换代码吗? 最好是“对除NameYear 之外的所有列执行此操作。

欢迎任何帮助。

【问题讨论】:

    标签: python pandas replace recode


    【解决方案1】:

    获取所有列名到变量并比较布尔掩码,然后通过转换为整数将True/False 转换为1/0

    cols = Table.columns.difference(['Name','Year'])
    Table[cols] = (Table[cols] >= 4).astype(int)
    

    numpy.where:

    Table[cols] = np.where(Table[cols] < 4, 0, 1)
    

    【讨论】:

      【解决方案2】:

      下面介绍了两种可能的解决方案

      • applymap 如果需要更复杂的功能
      • 您的逻辑是二进制、二进制真值矩阵并改回整数表示
      df = pd.DataFrame({**{"Name":np.random.choice(["this","that","other"],15),"Year":np.random.choice(range(1990,2021),15)},
                   **{f"V{i}":np.random.randint(1,5,15) for i in range(10)}})
      
      df2 = df.copy()
      # solution 1
      df.loc[:,[c for c in df.columns if c.startswith("V")]] = df.loc[:,[c for c in df.columns if c.startswith("V")]].applymap(lambda v: 0 if v<=3 else 1)
      # solution 2
      df2.loc[:,[c for c in df2.columns if c.startswith("V")]] = (df2.loc[:,[c for c in df2.columns if c.startswith("V")]]<=3).astype(int)
      
      
      Name Year V0 V1 V2 V3 V4 V5 V6 V7 V8 V9
      this 1998 0 1 0 0 1 0 0 0 0 0
      that 2010 1 0 0 0 0 1 0 0 1 0
      this 2004 0 0 0 0 1 0 0 1 0 0
      this 1992 0 1 1 0 0 1 0 0 1 1
      this 1990 0 0 1 0 0 0 0 0 0 1
      this 2020 0 0 1 1 0 1 0 1 0 1
      this 2016 0 1 0 0 0 0 1 0 1 0
      other 1997 1 0 0 0 1 1 0 0 1 0
      that 2000 1 0 1 0 0 1 1 0 0 0
      that 2020 0 0 1 0 1 0 0 0 0 1
      that 1991 0 0 0 0 0 0 1 0 0 1
      other 2015 0 0 0 0 0 0 1 1 0 0
      this 2020 0 0 0 1 0 0 0 0 0 0
      other 2005 1 0 0 0 1 0 1 0 0 0
      other 2008 1 0 0 0 0 0 1 0 0 0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多