简体   繁体   中英

Remove Empty DataFrame in python list

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?

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM