0

I have a dataframe similar to this:

df = pd.DataFrame({'A':[[(1,2), (3,4)], [(5,6), (7,8), (9,10)]]})

I would like to transform into this format:

df = pd.DataFrame({'a':[1,3,5,7,9], 'b':[2,4,6,8,10]})

So basically have all the tuples[0] into column A and the tuples1 into column B

I found this Stack Overflow question helpful : how to split column of tuples in pandas dataframe? but I have a list of tuples and not a single tuple in the columns.

Any help would be appreciated. Thank you

colla
  • 717
  • 1
  • 10
  • 22

2 Answers2

1

Use Series.explode with convert tuples to lists and then pass to DataFrame constructor:

df = pd.DataFrame(df['A'].explode().tolist(), columns=['A','B'])
print (df)
   A   B
0  1   2
1  3   4
2  5   6
3  7   8
4  9  10

Or use list comprehension with flatten nested lists:

df = pd.DataFrame([y for x in df['A'] for y in x], columns=['A','B'])
print (df)
   A   B
0  1   2
1  3   4
2  5   6
3  7   8
4  9  10

EDIT: If add new empty list both solutions working different:

df = pd.DataFrame({'A':[[(1,2), (3,4)], [(5,6), (7,8), (9,10)], []]})

df1 = pd.DataFrame(df['A'].explode().tolist(), columns=['A','B'])
print (df1)
     A     B
0  1.0   2.0
1  3.0   4.0
2  5.0   6.0
3  7.0   8.0
4  9.0  10.0
5  NaN   NaN

df1 = pd.DataFrame([y for x in df['A'] for y in x], columns=['A','B'])
print (df1)
   A   B
0  1   2
1  3   4
2  5   6
3  7   8
4  9  10
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

You can use numpy.vstack here.

df = pd.DataFrame(np.vstack(df["A"]), columns=["A", "B"])
# df

   A   B
0  1   2
1  3   4
2  5   6
3  7   8
4  9  10
Ch3steR
  • 20,090
  • 4
  • 28
  • 58