Jason Brown
Jason Brown

Reputation: 374

pandas collapse Series values into one value

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

Answers (3)

Grr
Grr

Reputation: 16119

I believe ' '.join(series) should do it

Upvotes: 1

Hallowizer
Hallowizer

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

Zero
Zero

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

Related Questions