Home > OS >  need to fill the non-null values to NaN based on column name
need to fill the non-null values to NaN based on column name

Time:12-29

enter image description here here need to update the non-null values of the columns values where specific text contains want to make them to nan values based on the column names. attached image for you reference.

CodePudding user response:

Consider the following dataset

enter image description here

Replace column name with nan. But this won't replace any other string e.g. X0 contains value oth.

df.apply(lambda s: s.replace({s.name: np.nan}))

enter image description here

Replace all string with nan

df.apply(lambda s: pd.to_numeric(s, errors='coerce'))

enter image description here

Replace all string with nan on subset of columns

COLS = ['X0', 'X2']
df.apply(lambda s: pd.to_numeric(s, errors='coerce') if s.name in COLS else s)

enter image description here

Note: I have used pandas apply function but same result can be achieved with for loop

CodePudding user response:

Is this what you are looking for?

import pandas as pd
import numpy as np

# Create a sample DataFrame
df = pd.DataFrame({'col1': [1, 'col1', 3, 4], 'col2': [5, 6, 'col2', 8]})

df = df.apply(lambda x: x.replace({x.name: np.nan}))

print(df)
  • Related