0

I changed the Nan values by iterating through the columns and after that i get RecursionError when i try to view the dataframe

for i in df.columns:
   df[i].fillna(df[i].mean, inplace=True)

df
 

Edit: Reproducible code

# import pandas and load cancer data
import pandas as pd 
import numpy as np

df = pd.read_csv("cancer_data_means.csv")

# check which columns have missing values with info()
df.info()

# use means to fill in missing values
for i in df.columns:
  df[i].fillna(df[i].mean, inplace=True)

# confirm your correction with info()
df.info()
df
DODE
  • 3
  • 2
  • Did you take a look at this: https://stackoverflow.com/questions/18689823/pandas-dataframe-replace-nan-values-with-average-of-columns ? – gudumoni Dec 09 '20 at 18:56
  • Please add a few test data to your code so that people may run it to reproduce the problem. See https://stackoverflow.com/help/minimal-reproducible-example. – bjhend Dec 09 '20 at 19:09
  • @gudumoni yeah but i want to know why i get this error. – DODE Dec 10 '20 at 19:10
  • @bjhend Thank you,Done. – DODE Dec 10 '20 at 19:14
  • @DODE That's way better, but we are still missing the test data, because we do not have the CSV file you load. Instead of the `pd.read_csv` command just assign some data directly to `df` that will trigger the error. And besides, it may also help to insert the exact error message into your question. – bjhend Dec 11 '20 at 20:26

1 Answers1

0

Generally changing the object that you are iterating over directly like this can cause weird behavior. You should be able to do it in a single step using

df.fillna({col: df[col].mean for col in df.columns}, axis=1)
Laggs
  • 386
  • 1
  • 5
  • yes i know i can use another ways i just want to why it causes this error thank you. – DODE Dec 10 '20 at 19:16