首页 > 解决方案 > 如何从列表列表中制作数据框

问题描述

我已经搜索过,但直到无法弄清楚如何从下面制作数据框:

0       ([179, 142, 176, 177, 176, 180, 180, 180, 180,...
1       ([353, 314, 349, 349, 344, 359, 359, 359, 359,...
2       ([535, 504, 535, 535, 535, 540, 540, 540, 540,...
3       ([711, 664, 703, 703, 703, 721, 721, 721, 721,...
4       ([850, 810, 822, 822, 842, 857, 857, 857, 857,.

以下是单个数据的外观

([179, 142, 176],
 ['Qtr- Oct-20','Qtr- Oct-20','Qtr- Oct-20',],
 ['High','Low','Close'],
 [43.8, 26.05,33.1])

我想要的是

      0  1           2               3
0   179 Qtr- Oct-20 High            43.8
1   142 Qtr- Oct-20 Low             26.05
2   176 Qtr- Oct-20 High_Volume     1123132
3   177 Qtr- Oct-20 High_Delivery   42499

我得到了什么

      0
0   ([179, 142, 176, 177, 176, 180, 180, 180, 180,...
1   ([353, 314, 349, 349, 344, 359, 359, 359, 359,...
2   ([535, 504, 535, 535, 535, 540, 540, 540, 540,...

标签: pythonpandasdataframe

解决方案


让我们做apply+ pd.Series.explode

pd.DataFrame(df['col'].tolist()).apply(pd.Series.explode).reset_index(drop=True)

     0            1      2      3
0  179  Qtr- Oct-20   High   43.8
1  142  Qtr- Oct-20    Low  26.05
2  176  Qtr- Oct-20  Close   33.1

注意:df['col']是数据框中包含列表列表的列。


推荐阅读