简体   繁体   English

删除 python 列表中的空 DataFrame

[英]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当我尝试使用to_csvto_excel导出此列表时,

"NoneType object has no attribute" “NoneType object 没有属性”

AttributeError results. AttributeError 结果。

So I've tried所以我试过了

dfclean = [x for x in df if x]

which resulted in这导致

'NoneType' object is not iterable TypeError 'NoneType' object 不可迭代 TypeError

I've also tried我也试过

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

which resulted in这导致

Name 'dfclean' is not defined NameError名称“dfclean”未定义 NameError

I've also tried我也试过

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

which yielded the same产生了相同的结果

'NoneType' object is not iterable TypeError 'NoneType' object 不可迭代 TypeError

In all these cases the output successfully displays the df result (a list that starts with EmptyDataFrame in the first line).在所有这些情况下,output 成功显示 df 结果(第一行以 EmptyDataFrame 开头的列表)。 Any suggestions to eliminate this Empty DataFrame, which presumably is the culprit for the NoneType errors above?有什么建议可以消除这个 Empty DataFrame,这可能是上述 NoneType 错误的罪魁祸首?

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和来自 pandas 的文档来阐明如何在此处使用 DataFrame.empty

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.我的 df 有一个 Empty DataFrame dfclean 没有解决。 Once this was addressed, my export was successful.解决此问题后,我的导出成功。 Thanks again!再次感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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