【问题标题】:Appending DF to the CSV - How to write everything explicitly without any conversion whatsoever? [duplicate]将 DF 附加到 CSV - 如何在不进行任何转换的情况下明确编写所有内容? [复制]
【发布时间】:2020-07-12 00:56:05
【问题描述】:

我搜索并找到了这个answer 虽然我理解答案,但我的问题是:是否有任何可能的方法可以将数据从 DF 显式写入 CSV 并且没有任何可能的转换?有没有办法做到这一点?

例如,值“.227”作为“022699999999999998”存储到CSV

我有这个简单的代码,用 BS 抓取一些数据后,我在 DF 中打开它,然后写入 CSV:

table = soup.find('table', id='awer')
df1 = pd.read_html(str(table))
df1[0]['seas'] = season
print(df1[0])
df1[0].to_csv('abc.csv', encoding='utf=8', index=False, header=None , mode='a')

为了验证,在附加之前我已经打印出 DF 并且一切都很好。因此,在将数据附加到 CSV 时正在进行某种转换。

有什么办法解决吗?

谢谢

【问题讨论】:

  • 持有您的floatcolumn 的类型是什么? ,你只需要使用float_format='%.3f'
  • 号码.227是存储为022699999999999998还是0.22699999999999998
  • @MisterMiyagi 我相信已经是float,他的意思是说0.227被存储为0.022699999999999998,这就是他提到.的原因
  • 嗨@MisterMiyagi,正如我所写,.227 存储为022699999999999998

标签: python pandas csv beautifulsoup


【解决方案1】:

你的意思是像df.astype('unicode')这样的东西吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2017-11-16
    相关资源
    最近更新 更多