Reputation: 374
How does one collapse a Series into one value? I tried pd.concat()
.
dict = {"Cities": ["Chicago", "Hong Kong", "London"], "People": ["John", "Mike", "Sue"]}
df = pd.Dataframe.from_dict(dict)
Cities People
0 Chicago John
1 Hong Kong Mike
2 London Sue
* desired output: *
"John Mike Sue"
Upvotes: 2
Views: 1955
Reputation: 56
Try this:
# Cannot be named dict, as dict is a type.
dictionary = {"Cities": ["Chicago", "Hong Kong", "London"], "People": ["John", "Mike", "Sue"]}
print(' '.join(dictionary['People'])
You don't have to type the first line, as it is a comment.
Upvotes: 0
Reputation: 77027
You could use string method Series.str.cat
, apart from ' '.join(df.People)
In [96]: df.People.str.cat(sep=' ')
Out[96]: 'John Mike Sue'
Upvotes: 5