Saltar al contenido


Django Data Cleaning and Presentation with Pandas


This Django project is designed to demonstrate how to leverage the Pandas library to retrieve data from CSV files, to give sorting, modifications, and formatting, and then present the processed data on a web page using Django's built-in functionality. The goal is to create a web application that can display structured and formatted data from CSV files in an easily digestible HTML format.

This project starts with a Django web application, which provides the foundation for building the user interface and serving HTML content. The project includes CSV files containing raw data that needs to be processed and presented. Pandas, a powerful data manipulation library in Python, is utilized to read, clean, sort, and format the CSV data. Data Retrieval and Processing: CSV files are read using Pandas' read_csv() function to create DataFrames. Data is sorted, filtered, or modified as needed using Pandas DataFrame methods. Data is formatted to improve readability and presentation. HTML Generation: The Pandas DataFrame is converted to an HTML table using the to_html() function. This HTML representation includes styling options to make the table visually appealing. Any additional information, charts, or visualizations can be added to the HTML page as needed. Django Templates: Django templates are used to create HTML templates that incorporate the Pandas-generated HTML table and other content. Views and URLs: Django views are defined to process the data and render the HTML template. URL routing is set up to map URLs to specific views. Enables quick data analysis and presentation without manual preprocessing.


The country that has won the most gold medals in summer games is United States . Using idxmax() function.
For comprehensive information and detailed instructions, please refer to the official documentation.
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.idxmax.html

df['Gold'][1:].idxmax()

The country with the most significant difference between their summer and winter gold medal counts is United States  the difference was of 880 . Using idxmax() & abs() functions.
For comprehensive information and detailed instructions, please refer to the official documentation.
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.idxmax.html

abs(df['Gold'][1:] - df['Gold.1'][1:])).idxmax()

Determine the countries that have won at least one gold medal in both the summer and winter games, and then identify which of these countries has the greatest relative difference between their summer and winter gold medal counts, considering their total gold medal count. Bulgaria  was the countrie with the biggest difference.
S u m m e r   G o l d W i n t e r   G o l d T o t a l   G o l d condition = (df['Gold'] >=1) & (df['Gold.1'] >=1)
new_df = df[condition]
(abs(new_df['Gold'] - new_df['Gold.1'])/new_df['Gold.2']).idxmax()


This function generates a Series named 'Points.' This Series represents a weighted value system, where each gold medal (Gold.2) is assigned a weight of 3 points, silver medals (Silver.2) carry 2 points, and bronze medals (Bronze.2) contribute 1 point. The function should return the resulting column as a Series object, with the country names serving as the indices.


Points
United States 5684
Soviet Union 2526
Great Britain 1574
Germany 1546
France 1500
Italy 1333
Sweden 1217
China 1120
Russia 1042
East Germany 1068
Hungary 962
Australia 923
Norway 985
Finland 895
Canada 846
Japan 866
Netherlands 727
Switzerland 630
Austria 569
Romania 572
South Korea 609
Poland 520
West Germany 459
Bulgaria 411
Cuba 420
Denmark 335
Czechoslovakia 327
Belgium 276
United Team of Germany 269
Unified Team 287
Spain 268
Ukraine 220
Greece 213
Brazil 184
New Zealand 203
Belarus 154
Turkey 191
Yugoslavia 171
Kenya 168
South Africa 148
Argentina 130
Czech Republic 134
Jamaica 131
Mexico 109
Iran 110
Kazakhstan 113
North Korea 90
Ethiopia 94
Estonia 77
Croatia 67
Slovenia 56
Ireland 55
Slovakia 58
Indonesia 49
Egypt 49
Azerbaijan 43
Latvia 47
India 50
Georgia 42
Mongolia 37
Thailand 44
Portugal 39
Nigeria 37
Morocco 39
Chinese Taipei 32
Lithuania 38
Uzbekistan 38
Colombia 29
Trinidad and Tobago 27
Mixed team 38
Algeria 27
Chile 24
Armenia 16
Bahamas 24
Venezuela 18
Australasia 22
Pakistan 19
Uruguay 16
Tunisia 19
Philippines 11
Serbia and Montenegro 17
Liechtenstein 15
Zimbabwe 18
Russian Empire 14
Puerto Rico 10
Serbia 11
Moldova 9
Israel 10
Uganda 14
Dominican Republic 14
Malaysia 9
Cameroon 12
Qatar 4
Ghana 5
Namibia 8
Costa Rica 7
Luxembourg 9
Lebanon 6
Iceland 6
Bohemia 5
Singapore 6
Peru 9
Tajikistan 4
Syria 6
Panama 5
Independent Olympic Participants 4
Saudi Arabia 4
Hong Kong 6
Kyrgyzstan 4
Afghanistan 2
Tanzania 4
Kuwait 2
Sri Lanka 4
Suriname 4
Haiti 3
Mozambique 4
British West Indies 2
Ecuador 5
Vietnam 4
Zambia 3
Virgin Islands 2
Botswana 2
United Arab Emirates 3
Eritrea 1
Ivory Coast 2
Cyprus 2
Senegal 2
Burundi 3
Netherlands Antilles 2
Niger 1
Paraguay 2
Djibouti 1
Montenegro 2
Gabon 2
Sudan 2
Macedonia 1
Mauritius 1
Grenada 3
Guatemala 2
Guyana 1
Iraq 1
Togo 1
Tonga 2
Bahrain 1
Barbados 1
Bermuda 1
df['Points'] = pd.Series(df['Gold.2'] * 3 + df['Silver.2'] * 2 + df['Bronze.2'])
df_points = pd.DataFrame(df['Points'])
points = df_points[1:].to_html(classes='table table-bordered table-striped', index=True)

Data Cleaning with Pandas Library



# Summer Gold Silver Bronze Total # Winter Gold.1 Silver.1 Bronze.1 Total.1 # Games Gold.2 Silver.2 Bronze.2 Combined total ID
United States 26 976 757 666 2399 22 96 102 84 282 48 1072 859 750 2681 USA
Soviet Union 9 395 319 296 1010 9 78 57 59 194 18 473 376 355 1204 URS
Great Britain 27 236 272 272 780 22 10 4 12 26 49 246 276 284 806 GBR
Germany 15 174 182 217 573 11 78 78 53 209 26 252 260 270 782 GER
France 27 202 223 246 671 22 31 31 47 109 49 233 254 293 780 FRA
Italy 26 198 166 185 549 22 37 34 43 114 48 235 200 228 663 ITA
Sweden 26 143 164 176 483 22 50 40 54 144 48 193 204 230 627 SWE
China 9 201 146 126 473 10 12 22 19 53 19 213 168 145 526 CHN
Russia 5 132 121 142 395 6 49 40 35 124 11 181 161 177 519 RUS
East Germany 5 153 129 127 409 6 39 36 35 110 11 192 165 162 519 GDR
Hungary 25 167 144 165 476 22 0 2 4 6 47 167 146 169 482 HUN
Australia 25 139 152 177 468 18 5 3 4 12 43 144 155 181 480 AUS
Norway 24 56 49 43 148 22 118 111 100 329 46 174 160 143 477 NOR
Finland 24 101 84 117 302 22 42 62 57 161 46 143 146 174 463 FIN
Canada 25 59 99 121 279 22 62 56 52 170 47 121 155 173 449 CAN
Japan 21 130 126 142 398 20 10 17 18 45 41 140 143 160 443 JPN
Netherlands 25 77 85 104 266 20 37 38 35 110 45 114 123 139 376 NED
Switzerland 27 47 73 65 185 22 50 40 48 138 49 97 113 113 323 SUI
Austria 26 18 33 35 86 22 59 78 81 218 48 77 111 116 304 AUT
Romania 20 88 94 119 301 20 0 0 1 1 40 88 94 120 302 ROU
South Korea 16 81 82 80 243 17 26 17 10 53 33 107 99 90 296 KOR
Poland 20 64 82 125 271 22 6 7 7 20 42 70 89 132 291 POL
West Germany 5 56 67 81 204 6 11 15 13 39 11 67 82 94 243 FRG
Bulgaria 19 51 85 78 214 19 1 2 3 6 38 52 87 81 220 BUL
Cuba 19 72 67 70 209 0 0 0 0 0 19 72 67 70 209 CUB
Denmark 26 43 68 68 179 13 0 1 0 1 39 43 69 68 180 DEN
Czechoslovakia 16 49 49 45 143 16 2 8 15 25 32 51 57 60 168 TCH
Belgium 25 37 52 53 142 20 1 1 3 5 45 38 53 56 147 BEL
United Team of Germany 3 28 54 36 118 3 8 6 5 19 6 36 60 41 137 EUA
Unified Team 1 45 38 29 112 1 9 6 8 23 2 54 44 37 135 EUN
Spain 22 37 59 35 131 19 1 0 1 2 41 38 59 36 133 ESP
Ukraine 5 33 27 55 115 6 2 1 4 7 11 35 28 59 122 UKR
Greece 27 30 42 39 111 18 0 0 0 0 45 30 42 39 111 GRE
Brazil 21 23 30 55 108 7 0 0 0 0 28 23 30 55 108 BRA
New Zealand 22 42 18 39 99 15 0 1 0 1 37 42 19 39 100 NZL
Belarus 5 12 24 39 75 6 6 4 5 15 11 18 28 44 90 BLR
Turkey 21 39 25 24 88 16 0 0 0 0 37 39 25 24 88 TUR
Yugoslavia 16 26 29 28 83 14 0 3 1 4 30 26 32 29 87 YUG
Kenya 13 25 32 29 86 3 0 0 0 0 16 25 32 29 86 KEN
South Africa 18 23 26 27 76 6 0 0 0 0 24 23 26 27 76 RSA
Argentina 23 18 24 28 70 18 0 0 0 0 41 18 24 28 70 ARG
Czech Republic 5 14 15 15 44 6 7 9 8 24 11 21 24 23 68 CZE
Jamaica 16 17 30 20 67 7 0 0 0 0 23 17 30 20 67 JAM
Mexico 22 13 21 28 62 8 0 0 0 0 30 13 21 28 62 MEX
Iran 15 15 20 25 60 10 0 0 0 0 25 15 20 25 60 IRI
Kazakhstan 5 16 17 19 52 6 1 3 3 7 11 17 20 22 59 KAZ
North Korea 9 14 12 21 47 8 0 1 1 2 17 14 13 22 49 PRK
Ethiopia 12 21 7 17 45 2 0 0 0 0 14 21 7 17 45 ETH
Estonia 11 9 9 15 33 9 4 2 1 7 20 13 11 16 40 EST
Croatia 6 6 7 10 23 7 4 6 1 11 13 10 13 11 34 CRO
Slovenia 6 4 6 9 19 7 2 4 9 15 13 6 10 18 34 SLO
Ireland 20 9 8 12 29 6 0 0 0 0 26 9 8 12 29 IRL
Slovakia 5 7 9 8 24 6 2 2 1 5 11 9 11 9 29 SVK
Indonesia 14 6 10 11 27 0 0 0 0 0 14 6 10 11 27 INA
Egypt 21 7 9 10 26 1 0 0 0 0 22 7 9 10 26 EGY
Azerbaijan 5 6 5 15 26 5 0 0 0 0 10 6 5 15 26 AZE
Latvia 10 3 11 5 19 10 0 4 3 7 20 3 15 8 26 LAT
India 23 9 6 11 26 9 0 0 0 0 32 9 6 11 26 IND
Georgia 5 6 5 14 25 6 0 0 0 0 11 6 5 14 25 GEO
Mongolia 12 2 9 13 24 13 0 0 0 0 25 2 9 13 24 MGL
Thailand 15 7 6 11 24 3 0 0 0 0 18 7 6 11 24 THA
Portugal 23 4 8 11 23 7 0 0 0 0 30 4 8 11 23 POR
Nigeria 15 3 8 12 23 0 0 0 0 0 15 3 8 12 23 NGR
Morocco 13 6 5 11 22 6 0 0 0 0 19 6 5 11 22 MAR
Chinese Taipei 13 2 7 12 21 11 0 0 0 0 24 2 7 12 21 TPE
Lithuania 8 6 5 10 21 8 0 0 0 0 16 6 5 10 21 LTU
Uzbekistan 5 5 5 10 20 6 1 0 0 1 11 6 5 10 21 UZB
Colombia 18 2 6 11 19 1 0 0 0 0 19 2 6 11 19 COL
Trinidad and Tobago 16 2 5 11 18 3 0 0 0 0 19 2 5 11 18 TRI
Mixed team 3 8 5 4 17 0 0 0 0 0 3 8 5 4 17 ZZX
Algeria 12 5 2 8 15 3 0 0 0 0 15 5 2 8 15 ALG
Chile 22 2 7 4 13 16 0 0 0 0 38 2 7 4 13 CHI
Armenia 5 1 2 9 12 6 0 0 0 0 11 1 2 9 12 ARM
Bahamas 15 5 2 5 12 0 0 0 0 0 15 5 2 5 12 BAH
Venezuela 17 2 2 8 12 4 0 0 0 0 21 2 2 8 12 VEN
Australasia 2 3 4 5 12 0 0 0 0 0 2 3 4 5 12 ANZ
Pakistan 16 3 3 4 10 2 0 0 0 0 18 3 3 4 10 PAK
Uruguay 20 2 2 6 10 1 0 0 0 0 21 2 2 6 10 URU
Tunisia 13 3 3 4 10 0 0 0 0 0 13 3 3 4 10 TUN
Philippines 20 0 2 7 9 4 0 0 0 0 24 0 2 7 9 PHI
Serbia and Montenegro 3 2 4 3 9 3 0 0 0 0 6 2 4 3 9 SCG
Liechtenstein 16 0 0 0 0 18 2 2 5 9 34 2 2 5 9 LIE
Zimbabwe 12 3 4 1 8 1 0 0 0 0 13 3 4 1 8 ZIM
Russian Empire 3 1 4 3 8 0 0 0 0 0 3 1 4 3 8 RU1
Puerto Rico 17 0 2 6 8 6 0 0 0 0 23 0 2 6 8 PUR
Serbia 3 1 2 4 7 2 0 0 0 0 5 1 2 4 7 SRB
Moldova 5 0 2 5 7 6 0 0 0 0 11 0 2 5 7 MDA
Israel 15 1 1 5 7 6 0 0 0 0 21 1 1 5 7 ISR
Uganda 14 2 3 2 7 0 0 0 0 0 14 2 3 2 7 UGA
Dominican Republic 13 3 2 1 6 0 0 0 0 0 13 3 2 1 6 DOM
Malaysia 12 0 3 3 6 0 0 0 0 0 12 0 3 3 6 MAS
Cameroon 13 3 1 1 5 1 0 0 0 0 14 3 1 1 5 CMR
Qatar 8 0 0 4 4 0 0 0 0 0 8 0 0 4 4 QAT
Ghana 13 0 1 3 4 1 0 0 0 0 14 0 1 3 4 GHA
Namibia 6 0 4 0 4 0 0 0 0 0 6 0 4 0 4 NAM
Costa Rica 14 1 1 2 4 6 0 0 0 0 20 1 1 2 4 CRC
Luxembourg 22 1 1 0 2 8 0 2 0 2 30 1 3 0 4 LUX
Lebanon 16 0 2 2 4 16 0 0 0 0 32 0 2 2 4 LIB
Iceland 19 0 2 2 4 17 0 0 0 0 36 0 2 2 4 ISL
Bohemia 3 0 1 3 4 0 0 0 0 0 3 0 1 3 4 BOH
Singapore 15 0 2 2 4 0 0 0 0 0 15 0 2 2 4 SIN
Peru 17 1 3 0 4 2 0 0 0 0 19 1 3 0 4 PER
Tajikistan 5 0 1 2 3 4 0 0 0 0 9 0 1 2 3 TJK
Syria 12 1 1 1 3 0 0 0 0 0 12 1 1 1 3 SYR
Panama 16 1 0 2 3 0 0 0 0 0 16 1 0 2 3 PAN
Independent Olympic Participants 1 0 1 2 3 0 0 0 0 0 1 0 1 2 3 IOP
Saudi Arabia 10 0 1 2 3 0 0 0 0 0 10 0 1 2 3 KSA
Hong Kong 15 1 1 1 3 4 0 0 0 0 19 1 1 1 3 HKG
Kyrgyzstan 5 0 1 2 3 6 0 0 0 0 11 0 1 2 3 KGZ
Afghanistan 13 0 0 2 2 0 0 0 0 0 13 0 0 2 2 AFG
Tanzania 12 0 2 0 2 0 0 0 0 0 12 0 2 0 2 TAN
Kuwait 12 0 0 2 2 0 0 0 0 0 12 0 0 2 2 KUW
Sri Lanka 16 0 2 0 2 0 0 0 0 0 16 0 2 0 2 SRI
Suriname 11 1 0 1 2 0 0 0 0 0 11 1 0 1 2 SUR
Haiti 14 0 1 1 2 0 0 0 0 0 14 0 1 1 2 HAI
Mozambique 9 1 0 1 2 0 0 0 0 0 9 1 0 1 2 MOZ
British West Indies 1 0 0 2 2 0 0 0 0 0 1 0 0 2 2 BWI
Ecuador 13 1 1 0 2 0 0 0 0 0 13 1 1 0 2 ECU
Vietnam 14 0 2 0 2 0 0 0 0 0 14 0 2 0 2 VIE
Zambia 12 0 1 1 2 0 0 0 0 0 12 0 1 1 2 ZAM
Virgin Islands 11 0 1 0 1 7 0 0 0 0 18 0 1 0 1 ISV
Botswana 9 0 1 0 1 0 0 0 0 0 9 0 1 0 1 BOT
United Arab Emirates 8 1 0 0 1 0 0 0 0 0 8 1 0 0 1 UAE
Eritrea 4 0 0 1 1 0 0 0 0 0 4 0 0 1 1 ERI
Ivory Coast 12 0 1 0 1 0 0 0 0 0 12 0 1 0 1 CIV
Cyprus 9 0 1 0 1 10 0 0 0 0 19 0 1 0 1 CYP
Senegal 13 0 1 0 1 5 0 0 0 0 18 0 1 0 1 SEN
Burundi 5 1 0 0 1 0 0 0 0 0 5 1 0 0 1 BDI
Netherlands Antilles 13 0 1 0 1 2 0 0 0 0 15 0 1 0 1 AHO
Niger 11 0 0 1 1 0 0 0 0 0 11 0 0 1 1 NIG
Paraguay 11 0 1 0 1 1 0 0 0 0 12 0 1 0 1 PAR
Djibouti 7 0 0 1 1 0 0 0 0 0 7 0 0 1 1 DJI
Montenegro 2 0 1 0 1 2 0 0 0 0 4 0 1 0 1 MNE
Gabon 9 0 1 0 1 0 0 0 0 0 9 0 1 0 1 GAB
Sudan 11 0 1 0 1 0 0 0 0 0 11 0 1 0 1 SUD
Macedonia 5 0 0 1 1 5 0 0 0 0 10 0 0 1 1 MKD
Mauritius 8 0 0 1 1 0 0 0 0 0 8 0 0 1 1 MRI
Grenada 8 1 0 0 1 0 0 0 0 0 8 1 0 0 1 GRN
Guatemala 13 0 1 0 1 1 0 0 0 0 14 0 1 0 1 GUA
Guyana 16 0 0 1 1 0 0 0 0 0 16 0 0 1 1 GUY
Iraq 13 0 0 1 1 0 0 0 0 0 13 0 0 1 1 IRQ
Togo 9 0 0 1 1 1 0 0 0 0 10 0 0 1 1 TOG
Tonga 8 0 1 0 1 1 0 0 0 0 9 0 1 0 1 TGA
Bahrain 8 0 0 1 1 0 0 0 0 0 8 0 0 1 1 BRN
Barbados 11 0 0 1 1 0 0 0 0 0 11 0 0 1 1 BAR
Bermuda 17 0 0 1 1 7 0 0 0 0 24 0 0 1 1 BER

Ver código