2

While I can read csv file instead of reading to whole file how can I print only certain rows and columns?

Imagine as if this is Excel:

  A              B              C                  D                    E
State  |Heart Disease Rate| Stroke Death Rate | HIV Diagnosis Rate |Teen Birth Rate

Alabama     235.5             54.5                 16.7                 18.01

Alaska      147.9             44.3                  3.2                  N/A    

Arizona     152.5             32.7                 11.9                  N/A    

Arkansas    221.8             57.4                 10.2                  N/A    

California  177.9             42.2                  N/A                  N/A    

Colorado    145.3             39                    8.4                 9.25    

Heres what I have:

import csv

try:
    risk = open('riskfactors.csv', 'r', encoding="windows-1252").read() #find the file

except:
    while risk != "riskfactors.csv":  # if the file cant be found if there is an error
    print("Could not open", risk, "file")
    risk = input("\nPlease try to open file again: ")
else:
    with open("riskfactors.csv") as f:
        reader = csv.reader(f, delimiter=' ', quotechar='|')

        data = []
        for row in reader:# Number of rows including the death rates 
            for col in (2,4): # The columns I want read   B and D
                data.append(row)
                data.append(col)
        for item in data:
            print(item) #print the rows and columns

I need to only read column B and D with all statistics to read like this:

  A              B                D                    
 State  |Heart Disease Rate| HIV Diagnosis Rate |

 Alabama       235.5             16.7                

  Alaska       147.9             3.2                     

  Arizona      152.5             11.9                     

  Arkansas     221.8             10.2                    

 California    177.9             N/A                     

 Colorado      145.3             8.4                

Edited

no errors

Any ideas on how to tackle this? Everything I try isn't working. Any help or advice is much appreciated.

2
  • TypeError: append() takes exactly one argument (2 given) You need to read and evaluate errors. Not ignore them. Commented Mar 8, 2013 at 4:13
  • I realized that and made the change but now its printing numbers and the whole list Commented Mar 8, 2013 at 4:17

3 Answers 3

11

I hope you have heard about Pandas for Data Analysis.

The following code will do the job for reading columns however about reading rows, you might have to explain more.

import pandas
io = pandas.read_csv('test.csv',sep=",",usecols=(1,2,4)) # To read 1st,2nd and 4th columns
print io 
Sign up to request clarification or add additional context in comments.

3 Comments

you know I tried import pandas on a different code few weeks ago and I kept on getting Traceback (most recent call last): File "C:/Users/Thomas/Desktop/Project 1.py", line 33, in <module> import pandas` ImportError: No module named 'pandas'
Did you installed Pandas in your pc and called with statement like "import pandas"
You were right of course. While using import pandas after I went through hell to install it made this a breeze! Thanks
3

If you're still stuck, there's really no reason you have to read the file with the CSV module as all CSV files are just comma separated strings. So, for something simple you could try this, which would give you a list of tuples of the form (state,heart disease rate,HIV diagnosis rate)

output = []

f = open( 'riskfactors.csv', 'rU' ) #open the file in read universal mode
for line in f:
    cells = line.split( "," )
    output.append( ( cells[ 0 ], cells[ 1 ], cells[ 3 ] ) ) #since we want the first, second and third column

f.close()

print output

Just note that you would then have to go through and ignore the header rows if you wanted to do any sort of data analysis.

1 Comment

After installing and using import pandas. this ran with no problem. Thanks for your guidance. This was very straight forward. :) I been stuck for a day with this.
2

try this

data = []
for row in reader:# Number of rows including the death rates
    data.append([row[1],row[3]) # The columns I want read  B and D
for item in data
            print(item) #print the rows and columns

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.