pandas将系列值折叠为一个值

时间:2016-07-13 19:14:00

标签: python pandas

如何将系列合并为一个值?我试过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

*所需的输出:*

"John Mike Sue"

3 个答案:

答案 0 :(得分:4)

' '.join(df.People)

外,您可以使用字符串方法Series.str.cat
In [96]: df.People.str.cat(sep=' ')
Out[96]: 'John Mike Sue'

答案 1 :(得分:1)

我相信' '.join(series)应该这样做

答案 2 :(得分:0)

试试这个:

# Cannot be named dict, as dict is a type.
dictionary = {"Cities": ["Chicago", "Hong Kong", "London"], "People": ["John", "Mike", "Sue"]}
print(' '.join(dictionary['People'])

您不必输入第一行,因为它是注释。