簡體   English   中英

列表的DataFrame到適當的系列

[英]DataFrame of lists to appropriate series

假設我有以下定義的數據幀df

df = pd.DataFrame([
        [[1, 2], [3, 4, 5]],
        [[6], [7, 8, 9, 0]]
    ], list('AB'), list('XY'))

我該怎么做到

A  X  0    1
      1    2
   Y  0    3
      1    4
      2    5
B  X  0    6
   Y  0    7
      1    8
      2    9
      3    0
dtype: int64

我試過了什么
我開始勸告這樣做的人。 那沒用。

調用stack幾次並應用pd.Series

df.stack().apply(pd.Series).stack().astype(int)

結果輸出:

A  X  0    1
      1    2
   Y  0    3
      1    4
      2    5
B  X  0    6
   Y  0    7
      1    8
      2    9
      3    0
dtype: int32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM