[英]How can I remove empty dataframes from a series of dataframes in pandas?
In python, I have a pandas series filled with pandas dataframes.在 python 中,我有一个充满熊猫数据框的熊猫系列。 However, sometimes an empty dataframe occurs in the series.
但是,有时系列中会出现空数据框。 Because they are causing trouble, I want to filter them out.
因为它们在制造麻烦,所以我想过滤掉它们。 I the following:
serie[not serie.empty]
, serie[!serie.empty]
, but both is giving me errors.我如下:
serie[not serie.empty]
, serie[!serie.empty]
,但两者都给我错误。 As alternative, I first replaced the empty data frames by 0, and then tried this: serie[serie != 0]
, but this is ambigu according to the error.作为替代方案,我首先将空数据帧替换为 0,然后尝试以下操作:
serie[serie != 0]
,但根据错误,这是不明确的。 Does anyone have any suggestions?有没有人有什么建议? Thanks in advance.
提前致谢。
A pandas Series has no empty
method.熊猫系列没有
empty
方法。 You can use map
to ask each element of the series to return its empty
attribute:您可以使用
map
要求系列的每个元素返回其empty
属性:
from operator import attrgetter
serie[~serie.map(attrgetter('empty'))]
A series of DataFrames?一系列数据帧?
You can check if a dataframe is empty with df.empty, so you could do您可以使用 df.empty 检查数据框是否为空,因此您可以这样做
serie[[not df.empty for df in serie]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.