Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas read csv file values as text without transforming to float first

I'm loading data from a csv file into pandas data frame, the column in the csv file contains these values:

1,2,3.4,5,6.2

The issue is that int values are changed to floats (I understand that this is because float will be the more general dtype), so it is read as:

df
   value
0    1.0
1    2.0
2    3.4
3    5.0
4    6.2

I need to get all the values as they are in the csv file as strings.

I tried using pd.read_csv('filename.csv',dtype=str) or pd.read_csv('filename.csv',dtype=object) and they result to changing them to floats then str:

  value
0  1.0
1  2.0
2  3.4
3  5.0
4  6.2

is there any way to read them as they are? i.e:

  value
0     1
1     2
2   3.4
3     5
4   6.2
like image 390
P. Naoum Avatar asked Oct 24 '25 07:10

P. Naoum


1 Answers

This is my test CSV (untitled.csv):

a,1

b,2

c,3.4

d,5

e,6.2

With the following code pd.read_csv('untitled.csv', dtype='str', header=None), I obtain:

enter image description here

like image 58
Stefano Fiorucci - anakin87 Avatar answered Oct 25 '25 19:10

Stefano Fiorucci - anakin87



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!