1

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?

1
  • 3
    Please, share want you have try so far. It will be easier to help you with a code snippet. Commented Apr 18, 2018 at 1:53

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.