简体   繁体   中英

unpack list in pandas dataframe python

I have a dataframe having column a,b,c. And column c has data in list datatype. So I want to unwind/unpacked/explode the each element of list as new row.

so, our input is like below.

   a  b             c
0  1  2          [1, {'k': 1}, 2]
1  3  2          [{'m': 2}, {'k': 2}, 2]

And our output should be like below.

   a  b          c
0  1  2          1
1  1  2          {'k': 1}
2  1  2          2
3  3  2          {'m': 2}
4  3  2          {'k': 2}
5  3  2          2

Sp, as you can in output, each element of list is in row and other column a and b values are repeating corresponding to list element

The solutions from the questions linked in the comments are outdated. These days you can just:

df.explode('c')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM