Panda Dataframe to.string 带小数点 (,)

Panda Dataframe to.string with a decimal point (,)

你能帮我解决以下任务吗?

我的数据框包含以下内容:

1 本 2 BIIB 3 岁 4重心 5 全球定位系统 6 哈尔 7 IPG 8 LY 9低 10 爱情 11 马克 12 PSX 13 最小值 14 回声 15 STT 16 UAA

我想将数据帧 df 保存为 txt 文件。但我需要以下格式的 txt 输出(以逗号分隔): BEN,BIIB,BMY,COG,GPS,HAL,IPG,LLY,LOW,LUV,MRK,PSX,RMD,ROP,STT,UAA

我尝试使用:

dfnew = df.to_string(decimal=str)

但这不是解决方案。

如果 dfSeries 通过 , 连接值并写入文件:

with open("file.txt", "w") as f:
    f.write(','.join(df))
        

如果值在列中:

with open("file.txt", "w") as f:
    f.write(','.join(df['column']))