Home > Blockchain >  converting a data frame cell into NaN using boolean mask
converting a data frame cell into NaN using boolean mask

Time:10-26

I have a dataframe(df) which looks like this:enter image description here

I want to convert all of the cells that have "..." into cells containing NaN. I created a boolean mask mask = df['Energy Supply per Capita'] !="…" to isolate the cells that have numbers but when I go to apply the mask df.where(mask).head() it converts the entire row into NaN instead of just the one cell so that df looks like this: enter image description here

how can I make it so that only the one specific cell is converted into NaN?

CodePudding user response:

You can use replace().

df.replace({"...": pd.NA}, inplace=True)

CodePudding user response:

Use numeric with errors coerce

cols = ['Energy Supply per Capita']
df[cols] = df[cols].apply(pd.to_numeric,errors='coerce')
  • Related