Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the .0 when reading CSV with Pandas

I have a CSV file im reading into pandas dataframe. All the numbers do not have any decimal places, but as soon as I read it into the dframe it adds a trailing zero to the number with a decimal.

1205 becomes 1205.0

How do I get rid of the 0 during pd.read_csv?

I know i can drop the .0 after it has been read into the dataframe, but i really need it not to happen at all.

I have tried float_precision='round_trip'

I have tried to force the dtype during read_csv

Some of the code i tried:

df = pd.read_csv('xxx.csv', header=None, dtype={'T': object,'Date': object,'VAL1': float, 'VAL2': float, 'VAL3': float, 'VAL4': float, 'VAL5': float})

OR

df = pd.read_csv('xxx.csv', header=None, float_precision='round_trip')
like image 519
dps Avatar asked Oct 24 '25 10:10

dps


2 Answers

You said you've tried to force dtype during read_csv, but I don't see why the following wouldn't solve your problem:

pd.read_csv('xxx.csv', dtype=str)
like image 68
Roméo Després Avatar answered Oct 26 '25 06:10

Roméo Després


Read the csv file:

import pandas as pd
file='cce_classification.csv'
df=csv_into_df(file)

Fill the nan with 0:

df=df.fillna(0)

Get the df columns and iterate it. change columntype into int by using try block:

for column in dfcolumnlist:
try:
    df[column]=df[column].astype(int)
except Exception as e:
    print(e)
print(df)

Final Code is:

file='cce_classification.csv'
def remove_dot_zeros_from_df(file):
   df=pd.read_csv(file)
   df=df.fillna(0)
   dfcolumnlist=df.columns
   for column in dfcolumnlist:
       try:
           df[column]=df[column].astype(int)
       except Exception as e:
           print(e)
   return df    
df=remove_dot_zeros_from_df(file)
like image 33
Ramesh Ponnusamy Avatar answered Oct 26 '25 06:10

Ramesh Ponnusamy