你能帮我解决下面的任务吗?
我的dataframe有以下内容:
1 BEN 2 BIIB 3 BMY 4 COG 5 GPS 6 HAL 7 IPG 8 LLY 9 LOW 10 LUV 11 MRK 12 PSX 13 RMD 14 ROP 15 STT 16 UAA
我想将dataframe 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)
但这不是解决之道。
发布于 2021-01-12 20:39:25
如果df
为Series
,则通过,
连接值并写入文件:
with open("file.txt", "w") as f:
f.write(','.join(df))
如果值在列中:
with open("file.txt", "w") as f:
f.write(','.join(df['column']))
https://stackoverflow.com/questions/65684103
复制相似问题