Search code examples
pandasnested-listsnested-datalist

Remove NaN values from pandas dataframes inside a list


I have a number of dataframes inside a list. I am trying to remove NaN values. I tried to do it in a for loop:

for i in list_of_dataframes:
    i.dropna()

it didn't work but python didnt return an error neither. If I apply the code

list_of_dataframes[0] = list_of_dataframes[0].dropna()

to a each dataframe individually it works, but i have too many of them. There must be a way which I just can't figure out. What are the possible solutions?

Thanks a lot


Solution

  • You didn't assign the new DataFrames with the dropped values to anything, so there was no effect.

    Try this:

    for i in range(len(list_of_dataframes)):
        list_of_dataframes[i] = list_of_dataframes[i].dropna()
    

    Or, more conveniently:

    for df in list_of_dataframes:
        df.dropna(inplace=True)