Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Using Excel CSV file to read only certain columns and rows

Tags:

python

file

excel

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.

like image 331
Thomas Jones Avatar asked Jun 07 '26 13:06

Thomas Jones


1 Answers

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 
like image 166
LonelySoul Avatar answered Jun 10 '26 02:06

LonelySoul



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!