[英]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.