Sininho
Sininho

Reputation: 296

Iterating through pandas string index turned them into floats

I have a csv file:

 SID    done    good_ecg    good_gsr    good_resp   comment
    436     0   1   1   
    2411        1   1   1   
    3858        0   1   1   
    4517        0   1   1   117 min diff between files
    9458        1   0   1   ######### error in my script
    9754        0   1   1   trigger fehler

        #REF!               
        88.8888888889   

Which I load in a pandas dataframe it like this:

df = pandas.read_csv(f ,delimiter="\t", dtype="str", index_col='SID')

I want to iterate through the index and print each one. But when I try

for subj in df.index:
   print subj

I get

436.0
2411.0
...

Now there is this '.0' at the end of each number. What am I doing wrong?

I have also tried iterating with iterrows() and have the same problem.

Thank you for any help!

EDIT: Here is the whole code I am using:

import pandas
def write(): 
   df = pandas.read_csv("overview.csv" ,delimiter="\t", dtype="str", index_col='SID')

   for subj in df.index: 
            print subj


write()

Upvotes: 3

Views: 264

Answers (1)

DSM
DSM

Reputation: 353059

Ah. The dtype parameter doesn't apply to the index_col:

>>> !cat sindex.csv
a,b,c
123,50,R
234,51,R
>>> df = pd.read_csv("sindex.csv", dtype="str", index_col="a")
>>> df
      b  c
a         
123  50  R
234  51  R
>>> df.index
Int64Index([123, 234], dtype='int64', name='a')

Instead, read it in without an index_col (None is actually the default, so you don't need index_col=None at all, but here I'll be explicit) and then set the index:

>>> df = pd.read_csv("sindex.csv", dtype="str", index_col=None)
>>> df = df.set_index("a")
>>> df
      b  c
a         
123  50  R
234  51  R
>>> df.index
Index(['123', '234'], dtype='object', name='a')

(I can't think of circumstances under which df.index would have dtype object but when you iterate over it you'd get integers, but you didn't actually show any self-contained code that generated that problem.)

Upvotes: 1

Related Questions