pandas to_csv将str列转换为int(或float) [英] pandas to_csv converts str column to int(or float)

查看:900
本文介绍了 pandas to_csv将str列转换为int(或float)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有注意到,pandas的"to_csv"会自动转换只有字母数字字符串要浮动的列. 我正在Jupyter笔记本中创建一个数据框,并创建一个充满值'1'的列['A'].因此,我有一个由字符串"1"的列组成的数据框. 当我将数据框转换为带有"to_csv"的csv文件时.输出的csv文件是一整列整数1.
您可能会建议我在jupyter中重新加载时将列转换为字符串,但是,这是行不通的,因为我事先不知道由于这种行为可能会惩罚哪些列. 有没有办法避免这种奇怪的情况.

As untitled, I noticed that pandas 'to_csv' transforms automatically columns where there are only alphanumerical strings to float . I am creating a dataframe in Jupyter notebook and creating a column ['A'] full of values '1'. Hence, I have a dataframe composed of a column of string '1'. When i convert my dataframe to csv file with 'to_csv'. the output csv file is a one column full of integers 1.
You may advise me to reconvert the column to string when reloaded in jupyter, However that's won't work because I don't know beforehand what columns may be penalized because of this behaviour. Is there a way to avoid this strange situation.

推荐答案

一种方法是分别存储您的类型并将其与数据一起加载:

One way is to store your types separately and load this with your data:

df = pd.DataFrame({0: ['1', '1', '1'],
                   1: [2, 3, 4]})

df.dtypes.to_frame('types').to_csv('types.csv')
df.to_csv('file.csv', index=False)

df_types = pd.read_csv('types.csv')['types']
df = pd.read_csv('file.csv', dtype=df_types.to_dict())

print(df.dtypes)
# 0    object
# 1     int64
# dtype: object

您可能希望考虑使用Pickle来确保您的数据帧保持不变:

You may wish to consider Pickle to ensure your dataframe is guaranteed to be unchanged:

df.to_pickle('file.pkl')
df = pd.read_pickle('file.pkl')

print(df.dtypes)
# 0    object
# 1     int64
# dtype: object

这篇关于 pandas to_csv将str列转换为int(或float)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆