Search code examples
pandastuplesseries

Dividing tuples in a series


How do I divide a panda series:

d = {'a': ('a','ball'), 'b': ('b','cat'), 'c': ('c','plane')}
ser = pd.Series(data=d, index=['a', 'b', 'c'])
ser

a     (a, ball)
b      (b, cat)
c    (c, plane)
dtype: object

Into a pandas dataframe like:

Index Value
a     ball
b     cat
c     plane

Solution

  • Use str[1] with Series.to_frame if need one column DataFrame:

    ser.str[1].to_frame('Value')
    

    Or if need 2 columns DataFrame:

    ser.str[1].rename_axis('Index').reset_index(name='Value')