简体   繁体   中英

Convert elements of a list in Dataframe to a column

I have a Dataframe as below:

                                 2021-02-01 00:00:00
0  [0.22081292351898762, 0.22248217707309176, 0.2...

I'd like to change it to:

   2021-02-01 00:00:00
0  0.22081292351898762
1  0.22248217707309176
2  0.2...
3  00000

Please give me some advises. Thank you.

lst = [] //your list
Use df = pd.DataFrame(lst)

Sample:

data = {'my_column': [
    [1, 2, 3],
    [4, 5, 6],
]}
df = pd.DataFrame(data)
print(df)
   my_column
0  [1, 2, 3]
1  [4, 5, 6]

Use explode :

df_explode = df.explode('my_column', ignore_index=True)
print(df_explode)
  my_column
0         1
1         2
2         3
3         4
4         5
5         6

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