Issaki
Issaki

Reputation: 1064

How to extract specific rows of a CSV file

I have a csv excel file that contains two columns named, "year_mo" and "vehicle_class".

year_mo, vehicle_class
2010-01, Category A
2010-02, Category B
2010-03, Category C
2011-01, Category A
2011-02, Category B
2011-03, Category C
2011-04, Category D
2011-05, Category E

I am using genfromtxt to load the file into my jupyter notebook. My goal is to extract all the rows that start from "2010". However, I am unable to do so. Below are my codes:

filename = "demoResults.csv"
data = np.genfromtxt(filename,
            dtype=["U50","U50"], delimiter="," ,names=True)

year = data["year_mo"]
year_2010 = year["2010" in year]
print(year_2010)

The result will print out an empty list.

Upvotes: 0

Views: 208

Answers (1)

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

This doesn't do what you think it does: "2010" in year

What you need to do is to check each element in the array:

year_2010 = [y for y in years if "2010" in y]

Upvotes: 1

Related Questions