Skip to content
Advertisement

Split Series of tuples into multiple columns

I have a series of tuples of the form ('Name', Number), and I would like to split them into two columns, one being the name, the other being the number.

I’d like to end up with something like this:

Tuple                   Name          Number
('Scott Smith', 56)     Scott Smith   56
('Anna Frank', 100)     Anna Frank    100
('Seth Morris', 32)     Seth Morris   32

I’ve tried a few iterations of splitting strings, applying a lambda function, etc, and can’t seem to get this simple process right.

Advertisement

Answer

Construct a new dataframe and assign back to dataframe

sample df:

               Tuple
0  (Scott Smith, 56)
1  (Anna Frank, 100)
2  (Seth Morris, 32)

df_final = df.assign(**pd.DataFrame(df.Tuple.tolist(), columns=['Name','Number']))

Out[170]:
               Tuple         Name  Number
0  (Scott Smith, 56)  Scott Smith      56
1  (Anna Frank, 100)   Anna Frank     100
2  (Seth Morris, 32)  Seth Morris      32
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement