[英]how to remove empty series from list in Python
我有一个看起来像这样的清单。
DB
Out[469]:
[[3 523
Name: order_id, dtype: object], 0 [526, 533]
Name: order_id, dtype: object, Series([], Name: order_id, dtype: object)]
而我希望它看起来像这样。
DB
[['523',['526','533']]]
我正在python中跟随
DB[0][0].values[0]
Out[462]: '523'
DB[1][0]
Out[463]: ['526', '533']
并删除空系列
[x for x in DB if x != []]
但这行不通。 我想要一个for循环,该循环将遍历DB
并提供最终输出。 请帮忙。
测试列表理解中的len
以将其删除:
In [150]:
l=[pd.Series(['asda']), pd.Series(), pd.Series([9,12,4])]
l
Out[150]:
[0 asda
dtype: object, Series([], dtype: float64), 0 9
1 12
2 4
dtype: int64]
In [153]:
[x for x in l if len(x)>0]
Out[153]:
[0 asda
dtype: object, 0 9
1 12
2 4
dtype: int64]
您可以看到长度不同:
In [155]:
print(len(l))
print(len([x for x in l if len(x)>0]))
3
2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.