我使用熊猫在csv上应用一些格式级别的更改,并将结果存储在目标文件中。源文件有一些整数,但是在熊猫操作之后,整数被转换为小数。例如,将源文件中的3转换为3.0。我希望整数保持为整数。
对如何让这件事起作用有什么建议吗?会很有帮助的,谢谢!
import pandas as pd
# reading the csv file
df = pd.read_csv(source)
# updating the column value/data
df['Regular'] = df['Regular'].replace({',': '_,'})
# writing into the file
df.to_csv(target, index=False)发布于 2022-08-31 15:11:56
您可以为熊猫read_csv()指定数据类型,例如:
df = pd.read_csv(source, dtype={'column_name_a': 'Int32', 'column_name_b': 'Int32'})参见此处的docs ::https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
https://stackoverflow.com/questions/73558034
复制相似问题