Pandas.to_csv 导致浮点数出现
Pandas.to_csv Causing Floats to Appear
我创建了一个程序,它接受 Excel 工作表,用 Pandas 修改它们,并将它们保存到 CSV。
当我检查输出 CSV 时,列被视为整数,但是当我上传到 Oracle 时,列变成浮点数。
输入 excel 中的某些列是诸如“3”“4”“1”之类的内容,但其他列包含货币值的实际浮点数。出于这个原因,我无法将 as_type(int).
投射到整个数据帧上
excel 个工作表数以百计,并且每个月都有不同的列名。所以按列应用 as_type(int) 将不起作用。
代码示例:
import pandas as pd
output_location = save_location + '.csv'
df_manipulation = pd.read_excel(filepath, index_col=None)
df_manipulation.to_excel(output_location, index = False)
关于如何将相关列保留为整数有什么想法吗?
阅读 documentation for read_excel(),特别是 convert_float 参数,它提到 Excel 在内部将所有数字存储为浮点数。
如果您想手动将列从 float 转换为 int,您可以这样做:
df_manipulation['column_name'] = df_manipulation['column_name'].astype('int')
或者,您可以使用特定数据类型加载整个 sheet,例如字符串:
df_manipulation = pd.read_excel(filepath, index_col=None, dtype = str)
我创建了一个程序,它接受 Excel 工作表,用 Pandas 修改它们,并将它们保存到 CSV。
当我检查输出 CSV 时,列被视为整数,但是当我上传到 Oracle 时,列变成浮点数。
输入 excel 中的某些列是诸如“3”“4”“1”之类的内容,但其他列包含货币值的实际浮点数。出于这个原因,我无法将 as_type(int).
投射到整个数据帧上excel 个工作表数以百计,并且每个月都有不同的列名。所以按列应用 as_type(int) 将不起作用。
代码示例:
import pandas as pd
output_location = save_location + '.csv'
df_manipulation = pd.read_excel(filepath, index_col=None)
df_manipulation.to_excel(output_location, index = False)
关于如何将相关列保留为整数有什么想法吗?
阅读 documentation for read_excel(),特别是 convert_float 参数,它提到 Excel 在内部将所有数字存储为浮点数。
如果您想手动将列从 float 转换为 int,您可以这样做:
df_manipulation['column_name'] = df_manipulation['column_name'].astype('int')
或者,您可以使用特定数据类型加载整个 sheet,例如字符串:
df_manipulation = pd.read_excel(filepath, index_col=None, dtype = str)