progNerd
progNerd

Reputation: 27

Remove specific values contained in DataFrame

I am trying to check if a specific value is contained anywhere in a certain column of my dataframe. I am using the following code, where it should clear data containing "0.0". However, it seemed like it is clearing data that does not contain "0.0" as well.

mydataset = mydataset[mydataset['Latitude'].astype(str).str.contains('0.0') == False]

Example of the data as follows. Highlighted in red are data being removed, upon applying the above code.

Sample Data

Upvotes: 0

Views: 27

Answers (2)

Rahul Patel
Rahul Patel

Reputation: 53

If you are using a pandas dataframe you can conditionally drop rows from your dataframe in the following manner:

mydataset = mydataset[str(mydataset.Latitude) != '0.0']

If you are trying to remove all 0 values and not just 0.0 then don't convert to string and it should drop any 0 value.

Upvotes: 0

jezrael
jezrael

Reputation: 863166

Here is problem . in regex is special char, so need regex=False or escape it by \, for invert mask use ~:

mydataset = mydataset[~mydataset['Latitude'].astype(str).str.contains('0.0', regex=False)]

Or:

mydataset = mydataset[~mydataset['Latitude'].astype(str).str.contains('0\.0')]

Upvotes: 1

Related Questions