Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress the scientific notation when pandas.read_csv()? [duplicate]

I want to convert .csv file to dataframe in Python. This is the codes.

import pandas as pd
csv_path = "./table.csv"
df = pd.read_csv(csv_path, error_bad_lines=False)

Below is original form of the .csv file when I opened it using Microsoft's notepad program.

"019E0001"

1    -   019E0001              -  ...            -           -   33312
2    -   019E0002              -  ...            -           -   43211
3    -   019E0003              -  ...            -           -   51588

But, when I read this .csv file in Python by using the function pandas.read_csv(), the value was converted to exponential form. "1.90E+02"

1    -   1.90E+02              -  ...            -           -   33312
2    -   1.90E+03              -  ...            -           -   43211
3    -   1.90E+04              -  ...            -           -   51588

I don't want that change.

I want to reserve the original text form (019E0001)

How could I deal with this problem??

Help me

like image 774
전은미 Avatar asked Oct 15 '25 08:10

전은미


1 Answers

Don't let Pandas infer datatype, so try to append dtype=str as argument of read_csv function:

import pandas as pd
csv_path = "./table.csv"
df = pd.read_csv(csv_path, error_bad_lines=False, dtype=str)  # <- HERE

After that, you can convert your other columns with the right dtype with df.astype({'colX': float, ...})

like image 78
Corralien Avatar answered Oct 16 '25 22:10

Corralien



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!