簡體   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