pandas 在使用read_csv时添加小数点 [英] Pandas adding decimal points when using read_csv

查看:228
本文介绍了 pandas 在使用read_csv时添加小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些csv文件,并使用熊猫将它们转换为数据框.之后,我使用输入来查找要删除的值

I'm working with some csv files and using pandas to turn them into a dataframe. After that, I use an input to find values to delete

我挂在一个小问题上:对于某些列,它在列中的值上添加了".o".它只在带有数字的列中执行此操作,因此我猜它正在以浮点数形式读取该列.如何防止这种情况发生?

I'm hung up on one small issue: for some columns it's adding ".o" to the values in the column. It only does this in columns with numbers, so I'm guessing it's reading the column as a float. How do I prevent this from happening?

真正让我感到困惑的部分是,它只发生在几列中,因此我无法完全弄清楚一种模式.我需要将".0"斩掉,以便重新导入它,并且我认为防止它首先发生是最容易的.

The part that really confuses me is that it only happens in a few columns, so I can't quite figure out a pattern. I need to chop off the ".0" so I can re-import it, and I feel like it would be easiest to prevent it from happening in the first place.

谢谢!

这是我的代码示例:

clientid = int(input('What client ID needs to be deleted?'))

df1 = pd.read_csv('Client.csv')
clientclean = df1.loc[df1['PersonalID'] != clientid]
clientclean.to_csv('Client.csv', index=None)

理想情况下,我希望所有值都与原始csv文件相同,但不要包含用户输入中带有clientid的行.

Ideally, I'd like all of the values to be the same as the original csv file, but without the rows with the clientid from the user input.

真正让我感到困惑的部分是,它只发生在几列中,因此我无法完全弄清楚一种模式.我需要将".0"斩掉,以便重新导入它,并且我认为防止它首先发生是最容易的.

The part that really confuses me is that it only happens in a few columns, so I can't quite figure out a pattern. I need to chop off the ".0" so I can re-import it, and I feel like it would be easiest to prevent it from happening in the first place.

谢谢!

推荐答案

这是数据类型问题.

ALollz的评论将我引向正确的方向.熊猫假设数据类型为float,会加上小数点.

ALollz's comment lead me in the right direction. Pandas was assuming a data type of float, which added the decimal points.

使用read_csv时,我将数据类型指定为对象(来自Akarius的评论),

I specified the datatype as object (from Akarius's comment) when using read_csv, which resolved the issue.

这篇关于 pandas 在使用read_csv时添加小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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