简体   繁体   English

如果包含负值,pandas 会删除整行

[英]pandas remove entire row if it contains negative values

How to remove the entire row if it contains negative values?如果整行包含负值,如何删除整行?

 df = pd.DataFrame({'col1': [1,-2,3,4],
                    'col2': [-1, -2, -3, 4],
                    'col4': [1,-2,3,4]})

output输出

       col1  col2  col4
        1    -1     1
        3    -3     3
        4     4     4

Or:或者:

>>> df[~df.lt(0).all(axis=1)]

   col1  col2  col4
0     1    -1     1
2     3    -3     3
3     4     4     4

Keep rows those at least one value is positive:保留那些至少有一个值为正的行:

df = df[df.ge(0).any(axis=1)]
print(df)

   col1  col2  col4
0     1    -1     1
2     3    -3     3
3     4     4     4

Or drop rows where all values are negative:或者删除所有值为负的行:

df = df.drop(df[df.lt(0).all(axis=1)].index)
print(df)

   col1  col2  col4
0     1    -1     1
2     3    -3     3
3     4     4     4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将负行值与前几行相加 pandas - Sum negative row values with previous rows pandas 熊猫按2列中的值过滤,返回整行 - pandas filter by values in 2 columns, return entire row 返回包含整行 Pandas DF 且具有 nan 值的元组列表 - Return list of tuples that contains entire row of Pandas DF with nan value Pandas 如果该行中的所有值都为假,则删除该行 - Pandas remove row if all values in that row are false 大熊猫:获得整个数据框中的最高值,以及行/列值? - pandas: get the highest values in an entire dataframe, and row/col values? 删除 NaN 'Cells' 而不删除整个 ROW (Pandas,Python3) - Remove NaN 'Cells' without dropping the entire ROW (Pandas,Python3) 熊猫连续检测连续的负数并返回值 - Pandas detecting consecutive negative numbers in a row, and returning values 如何排列 Pandas dataframe 基于所有行值为正,一行值为负,两行值为负...和所有行值为负, - How to arrange Pandas dataframe based on all row values positive, one row values negative, two row values negative ... and all row values negative, Python,Pandas:如何检查一行是否包含另一行中找到的值? - Python, Pandas: How to check if a row contains values found in another row? Python:将2d数组的每一行缩放为总和为1的值。每一行包含一些负值 - Python: Scale each row of 2d array to values that sum to 1. Each row contains some negative values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM