-1

I have a dataframe with one row like below:

col1 col2 col3
[a,b,c] [i,ii,iii] [1,2,3]

Now I need to transfer it into -

col1 col2 col3
a i 1
b ii 2
c iii 3

I have tried to use explode. But select clause allowed only one explode function which is very logical.

Can anyone help me with this?

2
  • There's an existing question about this for Pandas. But I'm not sure what you mean by "select clause" and I see comments about Pyspark. If the linked question isn't helpful, make a minimal reproducible example and LMK. For specifics, see How to make good reproducible pandas examples. Commented Jul 4, 2024 at 2:18
  • I was looking for a solution with pyspark actually. But solved it with pandas. Someone asked me to change the question. So did it. I am still looking for the solution with pyspark. Commented Jul 4, 2024 at 8:05

3 Answers 3

1

You can do this:

pd.DataFrame(dict(zip(df.columns, df.to_numpy().flatten())))

I think it is more efficient than apply/explode.

Sign up to request clarification or add additional context in comments.

Comments

1

Another possible solution (assuming that, by row, all lists have the same length):

df.explode(df.columns.to_list())

Output:

  col1 col2 col3
0    a    i    1
0    b   ii    2
0    c  iii    3

Comments

0

So the solution is:

df.apply(pd.Series.explode)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.