Bake.G
Bake.G

Reputation: 123

Get max value of column and associated row from CSV

I have a CSV file of crime statistics per year and I need to use Python to show each. In my first column I have the crime codes. The columns to the right are the number of instances of crime per year. I'm trying to find out the max value for each year, but return the crime code too.

So to start I've defined:

Year_2003 = crime_code[:, 1].astype(int)
max_crime_2003 = np.max(Year_2003)
print max_crime_2003

I'm aware that this literally gives me the maximum value of this column. Can anyone help to show me how to return the crime code located in the first column? I've searched the forum and found something on nested list so I've also included the following in my code:

nested_list = [row for row in readCSV]
np_list = np.array(nested_list, dtype = str)

crime_code = np_list[1:]

Thanks

Upvotes: 1

Views: 1107

Answers (1)

martianwars
martianwars

Reputation: 6500

You will need to use the np.argmax() function. This is how you can do it,

num_crimes = crime_code[:,1].astype(int)
codes = crime_code[:,0].astype(int)
code = codes[np.argmax(num_crimes)]

Upvotes: 2

Related Questions