Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas adding decimal points when using read_csv

Tags:

python

pandas

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

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?

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.

Thanks!

Here's a sample of my code:

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)

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.

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.

Thanks!

like image 780
clarktwain Avatar asked Jun 27 '26 19:06

clarktwain


1 Answers

If PersonalID if the header of the problematic column, try this:

df1 = pd.read_csv('Client.csv', dtype={'PersonalID':np.int32})

Edit: As there are no NaN value for integer. You can try this on each problematic colums:

df1[col] = df1[col].fillna(-9999) # or 0 or any value you want here
df1[col] = df1[col].astype(int)
like image 184
Akarius Avatar answered Jun 30 '26 10:06

Akarius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!