![](/img/trans.png)
[英]How to remove part of string ahead of special character in a column in 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=',')
在之间添加,但是如何为每个元素添加引号?
使用join
或Series.str.cat
:
string = ','.join("'"+df['type']+"'")
要么:
string = ("'"+df['type']+"'").str.cat(sep=',')
string
"'a','b','c'"
另一种选择是使用带有apply
和sum
format
:
result = df.type.apply("'{}',".format).sum()[:-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.