[英]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.