简体   繁体   中英

Pandas: Splitting JSON list value into new columns

I used Pandas to load a CSV to the following DataFrame:

      value                                  values                                   
0        56.0             [-0.5554548,10.0748005,4.232949]                          
1        72.0         [-0.1953888,0.15093994,-0.058532715] 
...

Now I would like to replace "values" column with 3 new columns like so:

     value     values_a      values_b      values_c                                      
0     56.0    -0.5554548    10.0748005     4.232949                          
1     72.0    -0.1953888    0.15093994    -0.058532715 
    ...

How can I split the list to 3 columns?

You can use split with removing [] by strip :

df1 = df.pop('values').str.strip('[]').str.split(',',expand=True).astype(float)
df[['values_a', 'values_b', 'values_c']] = df1

Solution if There is no NaN s:

L = [x.split(',') for x in df.pop('values').str.strip('[]').values.tolist()]
df[['values_a', 'values_b', 'values_c']] = pd.DataFrame(L).astype(float)

solutions with converting columns first to list and then is used DataFrame constructor:

import ast
s = df.pop('values').apply(ast.literal_eval)
df[['values_a', 'values_b', 'values_c']] = pd.DataFrame(s.values.tolist()).astype(float)

Similar:

df = pd.read_csv(file converters={'values':ast.literal_eval})
print (df)
   value                                  values
0   56.0      [-0.5554548, 10.0748005, 4.232949]
1   72.0  [-0.1953888, 0.15093994, -0.058532715]

df1 = pd.DataFrame(df.pop('values').tolist()).astype(float)
df[['values_a', 'values_b', 'values_c']] = df1

Final :

print (df)
   value  values_a   values_b  values_c
0   56.0 -0.555455  10.074801  4.232949
1   72.0 -0.195389   0.150940 -0.058533

EDIT:

If is possible in some column is more as 3 value then is not possible assign to 3 new columns. Solution is use join :

df = df.join(df1.add_prefix('val'))
print (df)
   value      val0       val1      val2
0   56.0 -0.555455  10.074801  4.232949
1   72.0 -0.195389   0.150940 -0.058533

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