【问题标题】:Panda Dataframe to.string with a decimal point (,)带有小数点 (,) 的 Panda Dataframe to.string
【发布时间】:2021-01-12 12:35:35
【问题描述】:

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

我的数据框有以下内容:

1 本 2 国际投资银行 3 BMY 4重心 5 全球定位系统 6 哈尔 7 IPG 8 LLY 9 低 10 卢夫 11 MRK 12 PSX 13RMD 14 机械臂 15 STT 16 阿联酋航空

我想将数据框 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)

但这不是解决方案。

【问题讨论】:

    标签: python pandas file export txt


    【解决方案1】:

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

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

    如果值在列中:

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

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2018-03-11
      • 2018-04-07
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多