Home > Software engineering >  Remove Empty DataFrame in python list
Remove Empty DataFrame in python list

Time:12-05

I have a list that starts with an "Empty DataFrame". When I tried to export this list with to_csv or to_excel, a

"NoneType object has no attribute"

AttributeError results.

So I've tried

dfclean = [x for x in df if x]

which resulted in

'NoneType' object is not iterable TypeError

I've also tried

dfclean = [df for df in dfclean if not df.empty]

which resulted in

Name 'dfclean' is not defined NameError

I've also tried

dfclean = list(filter(lambda df: not df.empty, df))

which yielded the same

'NoneType' object is not iterable TypeError

In all these cases the output successfully displays the df result (a list that starts with EmptyDataFrame in the first line). Any suggestions to eliminate this Empty DataFrame, which presumably is the culprit for the NoneType errors above?

CodePudding user response:

your list comprenhention its wrong that's why it throws the error.

first you initialize a value then you call it to loop and validate if it has conditions.

dfclean = [*df* for df in dfclean if not df.empty]

and more of this topic here

and de docs froms pandas to clarify how to use DataFrame.empty here

hope its help you.

CodePudding user response:

Thanks for your feedback. Turns out the problem was further upstream from my concatenation loop. This response solved my issue. My df had an Empty DataFrame which the dfclean did not address. Once this was addressed, my export was successful. Thanks again!

  • Related