【问题标题】:Replacing values greater 1 in a large pandas dataframe在大熊猫数据框中替换大于 1 的值
【发布时间】:2020-05-25 06:29:36
【问题描述】:

我试图用 1 替换所有大于 1 的数字,同时以最小的努力在整个数据框中保持原始 1 和 0 不变。感谢任何支持!

我的数据框看起来像这样,但包含更多的列和行。

Report No   Apple   Orange   Lemon   Grape   Pear
One           5       0        2       1      1
Two           1       1        0       3      2
Three         0       0        2       1      3
Four          1       1        3       0      0
Five          4       0        0       1      1
Six           1       3        1       2      0

期望的输出:

Report No   Apple   Orange   Lemon   Grape   Pear
One           1       0        1       1      1
Two           1       1        0       1      1
Three         0       0        1       1      1
Four          1       1        1       0      0
Five          1       0        0       1      1
Six           1       1        1       1      0

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    你可以试试这个。

    使用布尔掩码

    df.set_index('Report No',inplace=True)
    df[df > 1] = 1
    df.reset_index()
    
    Report No   Apple   Orange   Lemon   Grape   Pear
    One           1       0        1       1      1
    Two           1       1        0       1      1
    Three         0       0        1       1      1
    Four          1       1        1       0      0
    Five          1       0        0       1      1
    Six           1       1        1       1      0
    

    如果您有一些 非数字 列,也可以使用它。无需使用set_indexreset_index。这相当于df.select_dtypes('number')

    val = df._get_numeric_data()
    val[val > 1] = 1
    df
    Report No   Apple   Orange   Lemon   Grape   Pear
    One           1       0        1       1      1
    Two           1       1        0       1      1
    Three         0       0        1       1      1
    Four          1       1        1       0      0
    Five          1       0        0       1      1
    Six           1       1        1       1      0
    

    df.mask

    df.set_index('Report No',inplace=True)
    df.mask(df>1,1).reset_index()
    Report No   Apple   Orange   Lemon   Grape   Pear
    One           1       0        1       1      1
    Two           1       1        0       1      1
    Three         0       0        1       1      1
    Four          1       1        1       0      0
    Five          1       0        0       1      1
    Six           1       1        1       1      0
    

    np.where

    df[df.columns[1:]] = df.iloc[:,1:].where(df.iloc[:,1:] >1 ,1)
    

    np.select

    这在处理多个条件时会很有帮助。如果要将小于 0 的值转换为 0,将大于 1 的值转换为 1。

    df.set_index('Report No', inplace=True)
    condlist = [df >= 1, df <= 0] #you can have more conditions and add choices accordingly.
    choice = [1, 0] #len(condlist) should be equal to len(choice).
    df.loc[:] = np.select(condlist, choice)
    

    就像 Jan 提到的使用 df.clip


    不推荐,但您可以试试这个。使用df.astype

    df.set_index('Report No',inplace=True)
    df.astype('bool').astype('int')
    

    注意: 这只会将 falsy 值转换为 False 并将 truthy 值转换为 True 即这将转换 0False0 以外的任何东西都是True 甚至是负数。

    s = pd.Series([1,-1,0])
    s.astype('bool')
    0     True
    1     True
    2    False
    dtype: bool
    
    s.astype('bool').astype('int')
    0    1
    1    1
    2    0
    dtype: int32
    

    np.sign

    当存在的值介于 [0, n] 之间时,即没有负值。

    df.loc[:] = np.sign(df)
    

    【讨论】:

    • 为什么最后一种方法不推荐?
    【解决方案2】:

    使用pandas.DataFrame.clip:

    new_df = df.clip(0, 1)
    

    编辑:按名称排除第一列(这将就地编辑 DataFrame)

    mask = df.columns != "Report No"
    df.loc[:, mask] = df.loc[:, mask].clip(0, 1)
    

    【讨论】:

    • new_df = df.clip(0, 1) 效果不佳,因为“Report No”列是字符串。有什么方法可以隔离“​​报告编号”并执行 pandas.DataFrame.clip?
    • 查看其他答案。
    【解决方案3】:

    最快和最简单的方法是遍历 datframe 的所有键并使用 numpy 的 where 函数(必须导入的库)更改它们。然后,我们只需将条件和条件满足与否的值作为属性传递给该函数。在您的示例中,它看起来像这样:

    for x in df.keys()[1:]:
       df[x] = np.where(df[x] > 1, 1, df[x])
    

    请注意,在循环中我已经删除了第一个键,因为它的值不是整数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2018-11-23
      • 2021-09-29
      • 2018-09-29
      • 2022-06-24
      • 2022-01-11
      相关资源
      最近更新 更多