python - Pandas DataFrame 使用 where() 将列与阈值列进行比较

标签 python pandas

我需要将绝对值小于阈值列中相应值的几列中的值设为空

        import pandas as pd
        import numpy as np
        df=pd.DataFrame({'key1': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
          'key2': [2000, 2001, 2002, 2001, 2002], 
          'data1': np.random.randn(5),
          'data2': np.random.randn(5),
           'threshold': [0.5,0.4,0.6,0.1,0.2]}).set_index(['key1','key2'])

                   data1    data2   threshold
key1    key2            
Ohio    2000    0.201240    0.083833    0.5
        2001    -1.993489   -1.081208   0.4
        2002    0.759038    -1.688769   0.6
Nevada  2001    -0.543916   1.412679    0.1
        2002    -1.545781   0.181224    0.2

这给了我一个错误“无法在没有指定级别且没有重叠名称的情况下加入” df.where(df.abs()>df['阈值'])

这行得通,但显然是针对标量的 df.where(df.abs()>0.5)

                       data1           data2    threshold
        key1    key2            
        Ohio    2000    NaN              NaN    NaN
                2001    -1.993489   -1.081208   NaN
                2002    0.759038    -1.688769   NaN
      Nevada    2001    -0.543916   1.412679    NaN
                2002    -1.545781        NaN    NaN

顺便说一句,这似乎给了我一个不错的结果 - 仍然想知道如何用 where() 方法做到这一点

      df.apply(lambda x:x.where(x.abs()>x['threshold']),axis=1)

最佳答案

这里有一个使用 DataFrame.gt(大于)方法的选项略有不同。

df[df.abs().gt(df['threshold'], axis='rows')]
Out[16]: 
# Output might not look the same because of different random numbers,
# use np.random.seed() for reproducible random number gen
Out[13]: 
                data1     data2  threshold
key1   key2                               
Ohio   2000       NaN       NaN        NaN
       2001  1.954543  1.372174        NaN
       2002       NaN       NaN        NaN
Nevada 2001  0.275814  0.854617        NaN
       2002       NaN  0.204993        NaN

关于python - Pandas DataFrame 使用 where() 将列与阈值列进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26349926/

相关文章:

Python:查找并指向字符串的中点

python - Python Pandas 中的数据提取

Javascript 和 Django 'static' 模板标签

c++ - 从python/子进程调用另一个进程需要访问shell

python - Pandas 到 Excel(合并标题列)

python - 让 SQLite3 与多线程一起工作

python - Pandas 替换功能错误地更改了所有数据帧

python - 按出现次数分组

python - 在 Pandas 中创建基于列的连接名称和排名

python - 根据字符串是否是 pandas Dataframe 中的子字符串创建列