I am trying to join tuples in a column of my pandas dataframe and place that string in a new, separate column.
For example:
df = pd.DataFrame({'Number': ['001', '002', '003'],
'Tuple': [('hey', 'you'), ('you', 'can'), ('can', 'go')]})
I have tested the join using:
' '.join(_df.Tuple[0])
and the output looks like:
'hey you';
however, when I attempt to extend this to the rest of the Tuple column in the data frame with:
df['String'] = ' '.join([entry for entry in df.Tuple])
I receive a type error: TypeError: sequence item 0: expected str instance, tuple found. When I look into this error, I see an example that works for lists but not for dataframes. How can I iterate through a dataframe, joining the tuples in each row and place those strings into a new column?
The reason it doesn't work is because your list comprehension returns tuples which are immutable :
This ([entry for entry in df.Tuple])
returns
[('hey', 'you'), ('you', 'can'), ('can', 'go')]
a simpler method would be to use aggregation method on each item within the tuple on a row level.
df['Tuple'].agg(' '.join)
out:
0 hey you
1 you can
2 can go
Name: Tuple, dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With