0

I have the following python list,

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] ]

I want to concat it into a dataframe with axis=1, so it would be like that in the dataframe

<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

I used the following code:

frame = pd.concat(test_list, axis=1, ignore_index=True)

but I get the following error

TypeError: cannot concatenate object of type < class 'list' >
    only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid

1 Answer 1

2

First you convert to one dataframe , then groupby the ticker columns , then we do 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.