I would like to delete all rows with negative values (from an especific column). I´m trying this using the code below:
df = df.drop(columns = ['column_name'] < int(0)
And it´s showing the error 'SyntaxError: unexpected EOF while parsing'
What am I doing wrong?
CodePudding user response:
Try this:
df = df[df['column_name'] >= 0]
CodePudding user response:
Try this out
df = df.drop(df.index[df['col1'] < 0])
Also, instead of int(0) you can use 0.
CodePudding user response:
Let's start by creating a random dataframe
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(50, 4), columns=['A', 'B', 'C', 'D'])
[Out]:
A B C D
0 -0.776383 0.531066 -0.437706 1.297011
1 0.408125 0.924827 0.773968 0.482579
2 0.288039 1.367767 0.118284 -1.445995
3 -0.190943 0.753063 -1.224873 0.044104
4 1.057911 1.414593 -0.148705 0.495321
Now, considering that one wants each row to be greater than or equal to 0
, one has various ways to do that:
• Assuming one has multiple columns (with this method one can specify which columns we want with non-negative values), start by creating a list with the column names
columns = ['A', 'B', 'C', 'D']
And then
df_new = df[df[columns].min(axis=1) >= 0]
[Out]:
A B C D
1 0.408125 0.924827 0.773968 0.482579
14 0.926402 1.357251 1.072618 1.115446
49 0.967609 1.017095 0.535051 0.017753
• Assuming one wants to apply to every column, simply run
df_new = df[df.min(axis=1) >= 0]
[Out]:
A B C D
1 0.408125 0.924827 0.773968 0.482579
14 0.926402 1.357251 1.072618 1.115446
49 0.967609 1.017095 0.535051 0.017753