繁体   English   中英

将Pandas列转换为具有特殊字符分隔的字符串

[英]convert Pandas column to string with special character separation

df看起来像

  type
0 a
1 b
2 c

输出应该看起来像

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

我已经使用df.type.str.cat(sep=',')在之间添加,但是如何为每个元素添加引号?

使用joinSeries.str.cat

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

要么:

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

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

另一种选择是使用带有applysum format

result = df.type.apply("'{}',".format).sum()[:-1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM