Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert column values to NaN using np.where

I cannot figure out how to use the index results from np.where in a for loop. I want to use this for loop to ONLY change the values of a column given the np.where index results.

This is a hypothetical example for a situation where I want to find the indexed location of certain problems or anomalies in my dataset, grab their locations with np.where, and then run a loop on the dataframe to recode them as NaN, while leaving every other index untouched.

Here is my simple code attempt so far:

import pandas as pd
import numpy as np

# import iris
df = pd.read_csv('https://raw.githubusercontent.com/rocketfish88/democ/master/iris.csv')

# conditional np.where -- hypothetical problem data
find_error = np.where((df['petal_length'] == 1.6) & 
                  (df['petal_width'] == 0.2))

# loop over column to change error into NA
for i in enumerate(find_error):
    df = df['species'].replace({'setosa': np.nan})

# df[i] is a problem but I cannot figure out how to get around this or an alternative
like image 275
John Stud Avatar asked Oct 22 '25 06:10

John Stud


1 Answers

You can directly assign to the column:

m = (df['petal_length'] == 1.6) & (df['petal_width'] == 0.2)
df.loc[m, 'species'] = np.nan

Or, fixing your code.

df['species'] = np.where(m, np.nan, df['species'])

Or, using Series.mask:

df['species'] = df['species'].mask(m)
like image 119
cs95 Avatar answered Oct 23 '25 20:10

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!