![](/img/trans.png)
[英]Pandas: How to take values in a DataFrame column and put them all in the same row
[英]How to take set union of all the values in a column of pandas Dataframe?
數據幀的前兩行df
:
0|50331648|{1,2,3,4,5}|6
1|50331649|{3,5,7,8}|2
執行操作后,我只需要一個包含{1,2,3,4,5,7,8}
的集合。
如何實現?
假設"B"
是所考慮的列名,您可以在獲得的解壓列表上使用set.union
:
set.union(*df['B'].tolist())
{1, 2, 3, 4, 5, 7, 8}
(或者)
將這些作為可調用函數提供以reduce
:
from functools import reduce # If you're on Py3k
reduce(set.union, df['B'].tolist())
{1, 2, 3, 4, 5, 7, 8}
數據:
df = pd.DataFrame(dict(A=[50331648, 50331649],
B=[{1,2,3,4,5}, {3,5,7,8}],
C=[6,2])
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.