转换熊猫列字符串特殊字符分离

问题描述 投票:1回答:2

东风看起来像

  type
0 a
1 b
2 c

输出应该看起来像

string="'a','b','c'"

我已经使用df.type.str.cat(sep=',')增加,两者之间却怎么加引号的每个元素?

python pandas
2个回答
1
投票

使用joinSeries.str.cat

string = ','.join("'"+df['type']+"'")

要么:

string = ("'"+df['type']+"'").str.cat(sep=',')

string
"'a','b','c'"

1
投票

另一种选择是,与formatapply使用sum

result = df.type.apply("'{}',".format).sum()[:-1]
© www.soinside.com 2019 - 2024. All rights reserved.