code base 5000
code base 5000

Reputation: 4102

Pandas - create rows for entry in column

I have a returned datastructure that I want to explode into multiple rows.

For Example:

columns = ['A', 'B', 'C']
data = [1,['X','Y', 'Z'], 23]
df = pd.DataFrame(data, columns=columns)

This gives me 3 rows where one column is a list object.

I want to explode the row into 3 rows based on the column B. To get the r esult:

A    B    C
1    X    23
1    Y    23
1    Z    23

Is there a built in command to do this or a method out there?

Thanks

Upvotes: 1

Views: 46

Answers (1)

Zero
Zero

Reputation: 76917

You could use

In [114]: pd.DataFrame(dict(zip(columns, data)))
Out[114]:
   A  B   C
0  1  X  23
1  1  Y  23
2  1  Z  23

Upvotes: 3

Related Questions