我有以下python列表,
test_list =
[ ['CVM', 20010618, 332.5],
['CVM', 20010619, 332.5],
['CVM', 20010620, 330.0],
['CVM', 20010621, 342.5],
['CVM', 20010622, 337.5],
['AEF', 19970102, 18.7489],
['AEF', 19970103, 18.9735],
['AEF', 19970106, 19.5348],
['AEF', 19970107, 19.6471] ]
我想将其连接到axis = 1的数据框中,所以就像在数据框中一样
<TICKER><DTYYYYMMDD><CLOSE><TICKER><DTYYYYMMDD><CLOSE>
'CVM' 20010619 332.5 'AEF' 19970102 18.7489
'CVM' 20010620 330.0 'AEF' 19970103 18.9735
'CVM' 20010621 342.5 'AEF' 19970106 19.5348
'CVM' 20010622 337.5 'AEF' 19970107 19.6471
我使用了以下代码:
frame = pd.concat(test_list, axis=1, ignore_index=True)
但出现以下错误
TypeError: cannot concatenate object of type < class 'list' >
only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid
答案 0 :(得分:2)
首先,您将转换为一个数据帧,然后对代码行列进行分组,然后执行concat
pd.concat([y.reset_index(drop=True) for _, y in pd.DataFrame(test_list).groupby(0)],axis=1)
Out[351]:
0 1 2 0 1 2
0 AEF 19970102.0 18.7489 CVM 20010618 332.5
1 AEF 19970103.0 18.9735 CVM 20010619 332.5
2 AEF 19970106.0 19.5348 CVM 20010620 330.0
3 AEF 19970107.0 19.6471 CVM 20010621 342.5
4 NaN NaN NaN CVM 20010622 337.5