Replace Numeric Values In A Pandas Dataframe
Problem: Polluted Dataframe. Details: Frame consists of NaNs string values which i know the meaning of and numeric values. Task: Replaceing the numeric values with NaNs Example
Solution 1:
You can do a round-conversion to str
to replace the values and back.
df.astype('str').replace({'\d+': np.nan, 'nan': np.nan}, regex=True).astype('object')
#This makes sure already existing np.nan are not lost
Output
0120 abc cdf NaN1 k sum some
2NaNNaN nothing
Solution 2:
You can use a loop to go through each columns, and check each item. If it is an integer or float then replace it with np.nan. It can be done easily with map function applied on the column.
you can change the condition of the if
to incorporate any data type u want.
for x in df.columns:
df[x] = df[x].map(lambda item : np.nan iftype(item) == intortype(item) == floatelse item)
This is a naive approach and there have to be better solutions than this.!!
Post a Comment for "Replace Numeric Values In A Pandas Dataframe"