Skip to content
Advertisement

how to deal with multiple lists inside multiple column in df?

I have a df like this

                                            data_list
0   [['13878018', '13878274'], ['54211', '54212'], ['AARTIIND21JUL850PE', 'AARTIIND21JUL860CE'], ['AARTIIND', 'AARTIIND']]
1   [['13099778', '13100034'], ['51171', '51172'], ['ABFRL21JUL210PE', 'ABFRL21JUL215CE'], ['ABFRL', 'ABFRL']]
2   [['13910018', '13910274'], ['54336', '54337'], ['ACC21JUL1980PE', 'ACC21JUL2000CE'], ['ACC', 'ACC']]

and I want to convert it to

    name           token    ext_t      symbol
0   AARTIIND    13878018    54211       AARTIIND21JUL850PE
1   AARTIIND    13878274    54212       AARTIIND21JUL860CE
2   ABFRL       13099778    51171       ABFRL21JUL210PE
3   ABFRL       13100034    51172       ABFRL21JUL215CE
4   ACC         13910018    54336       ACC21JUL1980PE
5   ACC         13910274    54337       ACC21JUL2000CE

How can I achieve this?

I tried to apply pd.series and I got an output like this

                      0               1                                               2                         3
0  [13878018, 13878274]  [54211, 54212]        [AARTIIND21JUL850PE, AARTIIND21JUL860CE]      [AARTIIND, AARTIIND]
1  [13099778, 13100034]  [51171, 51172]              [ABFRL21JUL210PE, ABFRL21JUL215CE]            [ABFRL, ABFRL]
2  [13910018, 13910274]  [54336, 54337]                [ACC21JUL1980PE, ACC21JUL2000CE]                [ACC, ACC]

I am not sure how to proceed next. Can anyone help please?

Advertisement

Answer

try via DataFrame() method and apply():

out=pd.DataFrame(df['data_list'].tolist()).apply(pd.Series.explode)
#OR(you can also use agg() method in place of apply() method)
out=pd.DataFrame(df['data_list'].tolist()).agg(pd.Series.explode)

Finally:

out.columns=['token','ext_t','symbol','name']

Now If you print out you will get your expected output

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement