[英]Filter rows of pandas dataframe whose values are lower than 0
I have a pandas dataframe like this我有一个像这样的熊猫数据框
df = pd.DataFrame(data=[[21, 1],[32, -4],[-4, 14],[3, 17],[-7,NaN]], columns=['a', 'b'])
df
I want to be able to remove all rows with negative values in a list of columns and conserving rows with NaN.我希望能够删除列列表中具有负值的所有行并使用 NaN 保留行。
In my example there is only 2 columns, but I have more in my dataset, so I can't do it one by one.在我的示例中,只有 2 列,但我的数据集中有更多列,因此无法一一列出。
If you want to apply it to all columns, do df[df > 0]
with dropna()
:如果要将其应用于所有列,请使用
dropna()
执行df[df > 0]
:
>>> df[df > 0].dropna()
a b
0 21 1
3 3 17
If you know what columns to apply it to, then do for only those cols with df[df[cols] > 0]
:如果您知道要将其应用于哪些列,则仅对那些
df[df[cols] > 0]
:
>>> cols = ['b']
>>> df[cols] = df[df[cols] > 0][cols]
>>> df.dropna()
a b
0 21 1
2 -4 14
3 3 17
I've found you can simplify the answer by just doing this:我发现你可以通过这样做来简化答案:
>>> cols = ['b']
>>> df = df[df[cols] > 0]
dropna()
is not an in-place method, so you have to store the result. dropna()
不是就地方法,因此您必须存储结果。
>>> df = df.dropna()
I was looking for a solution for this that doesn't change the dtype (which will happen if NaN's are mixed in with ints as suggested in the answers that use dropna
. Since the questioner already had a NaN in their data, that may not be an issue for them. I went with this solution which preserves the int64
dtype. Here it is with my sample data:我正在寻找一个不改变 dtype 的解决方案(如果 NaN 与 int 混合,如使用
dropna
的答案中所建议的那样,就会发生这种情况。由于提问者的数据中已经有一个 NaN,这可能不是对他们来说是个问题。我采用了保留int64
dtype 的解决方案。这是我的示例数据:
df = pd.DataFrame(data={'a':[0, 1, 2], 'b': [-1,0,1], 'c': [-2, -1, 0]})
columns = ['b', 'c']
filter_ = (df[columns] >= 0).all(axis=1)
df[filter_]
a b c
2 2 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.