简体   繁体   中英

how to convert a list column in pandas dataframe?

I have a pandas dataframe in this format:

    idpso                                           pso
0   [1.0290574795443606, 20000, 3.515564441680908]  [0.041787490144988726, 20000, 11.214858293533325]

But I wanna to split each array into cells. Is there any way to do this?

You can using stack with apply

df=pd.DataFrame({'v1':[[1,2]],'v2':[[2,3]]})
df.stack().apply(pd.Series)
Out[638]: 
      0  1
0 v1  1  2
  v2  2  3

Use np.column_stack

Consider the sample data frame

df = pd.DataFrame(dict(
    idpso=[[1.0290, 20000, 3.5155]] * 3,
    pso=[[0.0417, 20000, 11.2148]] * 3
))

df

                    idpso                       pso
0  [1.029, 20000, 3.5155]  [0.0417, 20000, 11.2148]
1  [1.029, 20000, 3.5155]  [0.0417, 20000, 11.2148]
2  [1.029, 20000, 3.5155]  [0.0417, 20000, 11.2148]

Simple expansion

pd.DataFrame(
    np.column_stack(df.values.T.tolist())
)

       0        1       2       3        4        5
0  1.029  20000.0  3.5155  0.0417  20000.0  11.2148
1  1.029  20000.0  3.5155  0.0417  20000.0  11.2148
2  1.029  20000.0  3.5155  0.0417  20000.0  11.2148

pd.concat

pd.concat({
    k: pd.DataFrame(v.tolist())
    for k, v in df.items()
}, axis=1)

   idpso                    pso                
       0      1       2       0      1        2
0  1.029  20000  3.5155  0.0417  20000  11.2148
1  1.029  20000  3.5155  0.0417  20000  11.2148
2  1.029  20000  3.5155  0.0417  20000  11.2148

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