Issue
**I have a csv file in which I need fetch top 5 cities along with total casualties in that city and I have done most of the code but stuck how to Sort Dictionaries in desc-order which is inside numpy array.
**
city_dict = {}
with open('terrorismData.csv', 'r', encoding='utf-8') as file_obj:
Data = csv.DictReader(file_obj, skipinitialspace = True)
for row in Data:
if row['Country'] == 'India':
if row['Killed'] == '':
row['Killed'] = 0
if row['Wounded'] == '':
row['Wounded'] = 0
total_casuality = int(float(row['Killed'])) + int(float(row['Wounded']))
if row['City'] != 'Unknown':
if row['City'] in city_dict:
city_dict[row['City']] += total_casuality
else:
city_dict[row['City']] = total_casuality
np_city = np.array(city_dict)
print(np_city)
**The output (I get) on which I need to apply Sorting ** {'New Delhi': 2095, 'Samastipur': 4, 'Bombay': 210, 'Imphal': 603, 'Aizawl': 2, 'Amapur': 2, 'Raisikah': 1, 'Champhai': 1, 'Jamshedpur': 32, 'Chennai': 366, 'Chiaplant': 1, 'Tindol': 7, 'Calcutta': 57, 'Tirupattur': 6, 'Gauhati': 112, 'Jorhat': 3, 'Massad': 1, 'Chandigarh': 333, 'Jodhpur': 2, 'Amritsar': 768, 'Tipaimukh': 6, 'Guwahati': 822, 'Harchowal': 1, 'Mothan Wala': 2, 'Qadian': 7, 'Baloda Bazar': 10 }
The Output (I want). {'New Delhi': 2095, 'Amritsar': 768, 'Imphal': 603, 'Chandigarh': 333, Bombay': 210, .......'Harchowal': 1}
Solution
Your dictionary is:
city_dict = {'New Delhi': 2095, 'Samastipur': 4, 'Bombay': 210, 'Imphal': 603, 'Aizawl': 2, 'Amapur': 2, 'Raisikah': 1, 'Champhai': 1, 'Jamshedpur': 32, 'Chennai': 366, 'Chiaplant': 1, 'Tindol': 7, 'Calcutta': 57, 'Tirupattur': 6, 'Gauhati': 112, 'Jorhat': 3, 'Massad': 1, 'Chandigarh': 333, 'Jodhpur': 2, 'Amritsar': 768, 'Tipaimukh': 6, 'Guwahati': 822, 'Harchowal': 1, 'Mothan Wala': 2, 'Qadian': 7, 'Baloda Bazar': 10 }
np_city = np.array(city_dict)
print(np_city)
#output
array({'New Delhi': 2095, 'Samastipur': 4, 'Bombay': 210, 'Imphal': 603, 'Aizawl': 2, 'Amapur': 2, 'Raisikah': 1, 'Champhai': 1, 'Jamshedpur': 32, 'Chennai': 366, 'Chiaplant': 1, 'Tindol': 7, 'Calcutta': 57, 'Tirupattur': 6, 'Gauhati': 112, 'Jorhat': 3, 'Massad': 1, 'Chandigarh': 333, 'Jodhpur': 2, 'Amritsar': 768, 'Tipaimukh': 6, 'Guwahati': 822, 'Harchowal': 1, 'Mothan Wala': 2, 'Qadian': 7, 'Baloda Bazar': 10},
dtype=object)
Now, you have to access this dictionary from np.array
You have to do:
np_city.item()
To sort and reverse them, use np_city.item().items()
:
{key: value for key, value in sorted(np_city.item().items(), key=lambda item: item[1],reverse=True)}
#output
{'New Delhi': 2095, 'Guwahati': 822, 'Amritsar': 768, 'Imphal': 603, 'Chennai': 366, 'Chandigarh': 333, 'Bombay': 210, 'Gauhati': 112, 'Calcutta': 57, 'Jamshedpur': 32, 'Baloda Bazar': 10, 'Tindol': 7, 'Qadian': 7, 'Tirupattur': 6, 'Tipaimukh': 6, 'Samastipur': 4, 'Jorhat': 3, 'Aizawl': 2, 'Amapur': 2, 'Jodhpur': 2, 'Mothan Wala': 2, 'Raisikah': 1, 'Champhai': 1, 'Chiaplant': 1, 'Massad': 1, 'Harchowal': 1}
Answered By - Goku - stands with Palestine
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.