Я делаю Pandas проект по потреблению алкоголя. Для вашей информации в наборе данных есть следующие столбцы:
| Континент | Страна | Пиво | Дух | Вина |
Вот мой код:
# Separating data by continent
# ----------------------------
data_asia = data[data['Continent'] == 'Asia']
data_africa = data[data['Continent'] == 'Africa']
data_europe = data[data['Continent'] == 'Europe']
data_north = data[data['Continent'] == 'North America']
data_south = data[data['Continent'] == 'South America']
data_ocean = data[data['Continent'] == 'Oceania']
# Calculating n-largest for each category of drink
# ------------------------------------------------
top_5_asia_beer = data.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_asia_spir = data.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_asia_wine = data.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_asia_pure = data.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]
top_5_africa_beer = data.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_africa_spir = data.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_africa_wine = data.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_africa_pure = data.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]
top_5_europe_beer = data.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_europe_spir = data.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_europe_wine = data.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_europe_pure = data.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]
top_5_north_beer = data.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_north_spir = data.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_north_wine = data.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_north_pure = data.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]
top_5_south_beer = data.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_south_spir = data.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_south_wine = data.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_south_pure = data.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]
top_5_ocean_beer = data.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_ocean_spir = data.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_ocean_wine = data.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_ocean_pure = data.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]
Я понимаю нелепость моего кода с точки зрения дублирования и повторяемости. Кто-нибудь может поделиться советами и рекомендациями по рефакторингу кода?