0

I am reading a .csv file using pandas, this is my code:

import pandas as pd
df=pd.read_csv('MyFile.csv','r')
numeric_col=df.ix[:,0] #numeric values, works fine
string_col=df.ix[:,1] #string values, equals to nan

Does anyone know why I am not able to read the string column?

(or to be more accurate: I am able to read some string columns, but not others. For example, this is the first line of the csv:

20150329,3002,1,20000,32459,5100,10251181,DEADFALL,RAA,S,10251181,0

I am able to read col. 7 ('DEADFALL') but not col. 8 (RAA)).

5
  • 1
    Please show an extract of your original file. Commented Dec 21, 2015 at 9:10
  • this is the first line of the csv: 20150329,3002,1,20000,32459,5100,10251181,DEADFALL,RAA,S,10251181,0 actually I'm able to read col. 7 ('DEADFALL') but not col 8 (RAA), so maybe its not a problem with strings ... than what is the problem? Commented Dec 21, 2015 at 9:12
  • maybe add this to the question? Commented Dec 21, 2015 at 9:13
  • Does your CSV have a first-line header? Commented Dec 21, 2015 at 9:19
  • Where is df defined? Commented Dec 21, 2015 at 9:38

1 Answer 1

2

You can try to read the file in this way:

f = pd.read_csv('MyFile.csv',header=None)

Since it seems that your file has no header line. Your file should look then like this when reading:

         0     1   2      3      4     5         6         7    8  9   \
0  20150329  3002   1  20000  32459  5100  10251181  DEADFALL  RAA  S   

         10  11  
0  10251181   0 

Then you can access the single column by:

str_col = df[8]

or you can later rename the columns with different headers passing a list of headers string, for instance:

f.columns = [list_of_strings]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.