I have a df like this
JavaScript
x
5
1
data_list
2
0 [['13878018', '13878274'], ['54211', '54212'], ['AARTIIND21JUL850PE', 'AARTIIND21JUL860CE'], ['AARTIIND', 'AARTIIND']]
3
1 [['13099778', '13100034'], ['51171', '51172'], ['ABFRL21JUL210PE', 'ABFRL21JUL215CE'], ['ABFRL', 'ABFRL']]
4
2 [['13910018', '13910274'], ['54336', '54337'], ['ACC21JUL1980PE', 'ACC21JUL2000CE'], ['ACC', 'ACC']]
5
and I want to convert it to
JavaScript
1
8
1
name token ext_t symbol
2
0 AARTIIND 13878018 54211 AARTIIND21JUL850PE
3
1 AARTIIND 13878274 54212 AARTIIND21JUL860CE
4
2 ABFRL 13099778 51171 ABFRL21JUL210PE
5
3 ABFRL 13100034 51172 ABFRL21JUL215CE
6
4 ACC 13910018 54336 ACC21JUL1980PE
7
5 ACC 13910274 54337 ACC21JUL2000CE
8
How can I achieve this?
I tried to apply pd.series and I got an output like this
JavaScript
1
5
1
0 1 2 3
2
0 [13878018, 13878274] [54211, 54212] [AARTIIND21JUL850PE, AARTIIND21JUL860CE] [AARTIIND, AARTIIND]
3
1 [13099778, 13100034] [51171, 51172] [ABFRL21JUL210PE, ABFRL21JUL215CE] [ABFRL, ABFRL]
4
2 [13910018, 13910274] [54336, 54337] [ACC21JUL1980PE, ACC21JUL2000CE] [ACC, ACC]
5
I am not sure how to proceed next. Can anyone help please?
Advertisement
Answer
try via DataFrame()
method and apply()
:
JavaScript
1
4
1
out=pd.DataFrame(df['data_list'].tolist()).apply(pd.Series.explode)
2
#OR(you can also use agg() method in place of apply() method)
3
out=pd.DataFrame(df['data_list'].tolist()).agg(pd.Series.explode)
4
Finally:
JavaScript
1
2
1
out.columns=['token','ext_t','symbol','name']
2
Now If you print out
you will get your expected output