import numpy as np
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
import warnings
warnings.filterwarnings('ignore')
/kaggle/input/world-food-production/world food production.csv
/kaggle/input/latitude-and-longitude-for-every-country-and-state/world_country_and_usa_states_latitude_and_longitude_values.csv
/kaggle/input/country-mapping-iso-continent-region/continents2.csv
/kaggle/input/global-pollution-by-country-and-years/owid-co2-data.csv
import pandas as pd
location = pd.read_csv("/kaggle/input/latitude-and-longitude-for-every-country-and-state/world_country_and_usa_states_latitude_and_longitude_values.csv")
location.columns
Index(['country_code', 'latitude', 'longitude', 'country', 'usa_state_code',
'usa_state_latitude', 'usa_state_longitude', 'usa_state'],
dtype='object')
pollution = pd.read_csv("/kaggle/input/global-pollution-by-country-and-years/owid-co2-data.csv")
#pollution.head()
pollution.columns
Index(['country', 'year', 'iso_code', 'population', 'gdp', 'cement_co2',
'cement_co2_per_capita', 'co2', 'co2_growth_abs', 'co2_growth_prct',
'co2_including_luc', 'co2_including_luc_growth_abs',
'co2_including_luc_growth_prct', 'co2_including_luc_per_capita',
'co2_including_luc_per_gdp', 'co2_including_luc_per_unit_energy',
'co2_per_capita', 'co2_per_gdp', 'co2_per_unit_energy', 'coal_co2',
'coal_co2_per_capita', 'consumption_co2', 'consumption_co2_per_capita',
'consumption_co2_per_gdp', 'cumulative_cement_co2', 'cumulative_co2',
'cumulative_co2_including_luc', 'cumulative_coal_co2',
'cumulative_flaring_co2', 'cumulative_gas_co2', 'cumulative_luc_co2',
'cumulative_oil_co2', 'cumulative_other_co2', 'energy_per_capita',
'energy_per_gdp', 'flaring_co2', 'flaring_co2_per_capita', 'gas_co2',
'gas_co2_per_capita', 'ghg_excluding_lucf_per_capita', 'ghg_per_capita',
'land_use_change_co2', 'land_use_change_co2_per_capita', 'methane',
'methane_per_capita', 'nitrous_oxide', 'nitrous_oxide_per_capita',
'oil_co2', 'oil_co2_per_capita', 'other_co2_per_capita',
'other_industry_co2', 'primary_energy_consumption',
'share_global_cement_co2', 'share_global_co2',
'share_global_co2_including_luc', 'share_global_coal_co2',
'share_global_cumulative_cement_co2', 'share_global_cumulative_co2',
'share_global_cumulative_co2_including_luc',
'share_global_cumulative_coal_co2',
'share_global_cumulative_flaring_co2',
'share_global_cumulative_gas_co2', 'share_global_cumulative_luc_co2',
'share_global_cumulative_oil_co2', 'share_global_cumulative_other_co2',
'share_global_flaring_co2', 'share_global_gas_co2',
'share_global_luc_co2', 'share_global_oil_co2',
'share_global_other_co2', 'share_of_temperature_change_from_ghg',
'temperature_change_from_ch4', 'temperature_change_from_co2',
'temperature_change_from_ghg', 'temperature_change_from_n2o',
'total_ghg', 'total_ghg_excluding_lucf', 'trade_co2',
'trade_co2_share'],
dtype='object')
food = pd.read_csv("/kaggle/input/world-food-production/world food production.csv")
food.head
<bound method NDFrame.head of Entity Year Maize Production (tonnes)
0 Afghanistan 1961 700000.0
1 Afghanistan 1962 700000.0
2 Afghanistan 1963 713000.0
3 Afghanistan 1964 720000.0
4 Afghanistan 1965 720000.0
... ... ... ...
11907 Zimbabwe 2017 1532572.0
11908 Zimbabwe 2018 1560100.0
11909 Zimbabwe 2019 773976.0
11910 Zimbabwe 2020 1202347.0
11911 Zimbabwe 2021 1469664.0
Rice Production ( tonnes) Yams Production (tonnes) \
0 319000.00 7.467702e+06
1 319000.00 7.420515e+06
2 319000.00 8.479074e+06
3 380000.00 9.113779e+06
4 380000.00 1.006791e+07
... ... ...
11907 20502618.00 2.673608e+05
11908 31871776.60 2.667498e+05
11909 3382151.40 4.790022e+05
11910 3535631.80 7.437612e+05
11911 7575899.96 1.153263e+06
Wheat Production (tonnes) Tomatoes Production (tonnes) \
0 2279000.00 1873812.000
1 2279000.00 2044797.000
2 1947000.00 2096077.000
3 2230000.00 2388264.000
4 2282000.00 2559608.000
... ... ...
11907 30991516.16 324020.864
11908 16884482.98 309316.252
11909 64989733.20 540128.592
11910 43020887.20 5635740.200
11911 1316724.80 206550.000
Tea Production ( tonnes ) Sweet potatoes Production (tonnes) \
0 56315.0 3270871.000
1 61519.0 3562524.000
2 63596.0 3409916.000
3 66604.0 3229336.000
4 72418.0 3169104.000
... ... ...
11907 83904.6 874.408
11908 66842.6 1383.792
11909 9854.4 298831.560
11910 276279.0 1727311.800
11911 145563.0 262706.284
Sunflower seed Production (tonnes) ... Oranges Production (tonnes) \
0 12000.000 ... 10100.000
1 12800.000 ... 10100.000
2 12800.000 ... 10100.000
3 12800.000 ... 12400.000
4 13200.000 ... 13700.000
... ... ... ...
11907 4077308.066 ... 129055.800
11908 2107639.306 ... 168074.000
11909 3320915.400 ... 1134136.812
11910 983256.000 ... 72179.400
11911 421789.600 ... 3597076.600
Peas, dry Production ( tonnes) Palm oil Production (tonnes) \
0 232910.000 1131882.00
1 259412.000 1111006.00
2 251529.000 1145004.00
3 247556.000 1160831.00
4 266947.000 1138860.00
... ... ...
11907 40749.600 661978.98
11908 21606.400 485223.38
11909 45436.400 361128.20
11910 12023.200 279613.40
11911 26225.256 11000.00
Grapes Production (tonnes) Coffee, green Production ( tonnes) \
0 2.250000e+05 870970.000
1 2.250000e+05 883512.000
2 2.250000e+05 996674.000
3 2.650000e+05 1162048.000
4 2.870000e+05 1075084.000
... ... ...
11907 7.018985e+06 63.200
11908 3.415107e+06 68.000
11909 9.645766e+06 30401.400
11910 1.131937e+07 574723.374
11911 3.834786e+05 590070.590
Cocoa beans Production (tonnes) Meat, chicken Production (tonnes) \
0 835368.000 5600.00
1 867170.000 6000.00
2 922621.000 6160.00
3 1190061.000 6400.00
4 874245.000 6800.00
... ... ...
11907 34945.000 75000.00
11908 34659.800 80357.14
11909 20961.132 79790.97
11910 158022.122 8000.00
11911 2696.200 8000.00
Bananas Production ( tonnes) Avocados Production (tonnes) \
0 3139079.000 63439.000
1 3181580.000 65118.000
2 3304256.000 61760.000
3 3392527.000 62759.000
4 3450849.000 66269.000
... ... ...
11907 1267.200 145042.096
11908 1016.776 143384.384
11909 195845.198 525095.272
11910 1523258.600 28946.840
11911 425303.698 29313.660
Apples Production (tonnes)
0 15100.000
1 15100.000
2 15100.000
3 18400.000
4 20400.000
... ...
11907 1579154.000
11908 907419.000
11909 3117813.560
11910 7994169.220
11911 112519.364
[11912 rows x 24 columns]>
merged_df = pd.merge(pollution, food, left_on=['country', 'year'], right_on=['Entity', 'Year'], how='outer')
merged_df.drop(columns=["Entity"],inplace=True)
import pandas as pd
food['Gesamtproduktion (tonnes)'] = (
food['Maize Production (tonnes)'] +
food['Rice Production ( tonnes)'] +
food['Yams Production (tonnes)'] +
food['Wheat Production (tonnes)'] +
food['Tomatoes Production (tonnes)'] +
food['Tea Production ( tonnes )'] +
food['Sweet potatoes Production (tonnes)'] +
food['Sunflower seed Production (tonnes)'] +
food['Oranges Production (tonnes)'] +
food['Peas, dry Production ( tonnes)'] +
food['Palm oil Production (tonnes)'] +
food['Grapes Production (tonnes)'] +
food['Coffee, green Production ( tonnes)'] +
food['Cocoa beans Production (tonnes)'] +
food['Meat, chicken Production (tonnes)'] +
food['Bananas Production ( tonnes)'] +
food['Avocados Production (tonnes)'] +
food['Apples Production (tonnes)']
)
merged_df = pd.merge(pollution, food, left_on=['country', 'year'], right_on=['Entity', 'Year'], how='outer')
merged_df.drop(columns=["Entity"],inplace=True)
merged_df.drop(columns=["Year"],inplace=True)
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
import numpy as np
food_data = food[['Entity', 'Gesamtproduktion (tonnes)', 'Year']]
food_data.columns = ['Country', 'Gesamtproduktion', 'Year']
target_year = 2019
food_data = food_data[food_data['Year'] == target_year]
colorscale_custom = [
[0.0, 'rgb(255,255,255)'],
[0.2, 'rgb(255,200,200)'],
[0.4, 'rgb(255,150,150)'],
[0.6, 'rgb(255,100,100)'],
[0.8, 'rgb(255,50,50)'],
[1.0, 'rgb(255,0,0)']
]
fig_food = go.Figure(go.Choropleth(
locations=food_data['Country'],
z=np.sqrt(food_data['Gesamtproduktion']),
locationmode='country names',
colorscale=colorscale_custom,
colorbar=dict(title='Gesamtproduktion in Tonnen(√)'),
zmin=np.sqrt(food_data['Gesamtproduktion']).min(),
zmax=np.sqrt(food_data['Gesamtproduktion']).max(),
marker_line_color='black',
marker_line_width=0.5,
))
fig_food.update_geos(
projection_type='orthographic',
showocean=True,
oceancolor='lightblue',
showland=True,
landcolor='white',
showcountries=True,
countrycolor='black'
)
fig_food.update_layout(
title=f'Länder mit der höchsten Gesamtproduktion an Lebensmitteln im Jahr {target_year}',
hovermode=False
)
fig_food.show()
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
import numpy as np
pollution_data = pollution[['country', 'co2', 'year']]
pollution_data.columns = ['Country', 'CO2_Emissions', 'Year']
Auswahl des Zieljahres
target_year = 2019
pollution_data = pollution_data[pollution_data['Year'] == target_year]
def transform_log(x):
if x <= 0:
return 0
else:
return min(10, np.log10(x))
pollution_data['log_CO2'] = pollution_data['CO2_Emissions'].apply(transform_log)
fig_pollution = go.Figure(go.Choropleth(
locations=pollution_data['Country'],
z=pollution_data['log_CO2'],
locationmode='country names',
colorscale='Reds',
colorbar=dict(title='CO2 Emissions'),
zmin=0,
zmax=10,
marker_line_color='black',
marker_line_width=0.5,
))
fig_pollution.update_geos(
projection_type='orthographic',
showocean=True,
oceancolor='lightblue',
showland=True,
landcolor='white',
showcountries=True,
countrycolor='black'
)
fig_pollution.update_layout(
title=f'Länder mit den höchsten CO2-Emissionen im Jahr {target_year}',
hovermode=False
)
fig_pollution.show()
pollution_data.tail
<bound method NDFrame.tail of Country CO2_Emissions Year log_CO2
169 Afghanistan 11.082 2019 1.044618
442 Africa 1457.568 2019 3.163629
615 Africa (GCP) 1457.553 2019 3.163624
788 Albania 4.827 2019 0.683677
961 Algeria 178.577 2019 2.251826
... ... ... ... ...
47262 Wallis and Futuna 0.026 2019 -1.585027
47535 World 37040.102 2019 4.568672
47708 Yemen 11.837 2019 1.073242
47881 Zambia 7.751 2019 0.889358
48054 Zimbabwe 9.775 2019 0.990117
[259 rows x 4 columns]>
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display, clear_output
def plot_country_data(country_name):
country_data = merged_df[merged_df['country'] == country_name]
country_data_cleaned = country_data.dropna(subset=['year', 'co2', 'Gesamtproduktion (tonnes)'])
country_data_cleaned['co2'] = country_data_cleaned['co2'] * 10000000 # Multipliziere CO2-Werte
country_data_cleaned = country_data_cleaned.sort_values(by='year')
plt.gca().cla()
plt.plot(country_data_cleaned['year'], country_data_cleaned['co2'], label='CO2-Emissionen', color='blue', marker='o')
plt.plot(country_data_cleaned['year'], country_data_cleaned['Gesamtproduktion (tonnes)'], label='Lebensmittelproduktion', color='green', marker='s')
plt.title(f'CO2-Emissionen und Lebensmittelproduktion in {country_name}')
plt.xlabel('Jahr')
plt.ylabel('Werte')
plt.grid(True)
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
available_countries = merged_df['country'].unique()
country_dropdown = widgets.Dropdown(
options=available_countries,
value='Zimbabwe',
description='Land:',
disabled=False,
)
def update_plot(change):
print(f"Dropdown-Auswahl geändert: {change.new}")
plot_country_data(change.new)
country_dropdown.observe(update_plot, names='value')
display(country_dropdown)
plot_country_data(country_dropdown.value)
Land:
Zimbabwe
merged_df
country year iso_code population gdp cement_co2 cement_co2_per_capita co2 co2_growth_abs co2_growth_prct ... Peas, dry Production ( tonnes) Palm oil Production (tonnes) Grapes Production (tonnes) Coffee, green Production ( tonnes) Cocoa beans Production (tonnes) Meat, chicken Production (tonnes) Bananas Production ( tonnes) Avocados Production (tonnes) Apples Production (tonnes) Gesamtproduktion (tonnes)
0 Afghanistan 1850.0 AFG 3752993.0 NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 Afghanistan 1851.0 AFG 3767956.0 NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 Afghanistan 1852.0 AFG 3783940.0 NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 Afghanistan 1853.0 AFG 3800954.0 NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 Afghanistan 1854.0 AFG 3818038.0 NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
50253 Zimbabwe 2018.0 ZWE 15052191.0 2.271535e+10 0.558 0.037 10.715 1.419 15.265 ... 21606.400 485223.38 3.415107e+06 68.000 34659.800 80357.14 1016.776 143384.384 907419.000 5.832521e+07
50254 Zimbabwe 2019.0 ZWE 15354606.0 NaN 0.473 0.031 9.775 -0.939 -8.765 ... 45436.400 361128.20 9.645766e+06 30401.400 20961.132 79790.97 195845.198 525095.272 3117813.560 8.895097e+07
50255 Zimbabwe 2020.0 ZWE 15669663.0 NaN 0.496 0.032 7.850 -1.926 -19.700 ... 12023.200 279613.40 1.131937e+07 574723.374 158022.122 8000.00 1523258.600 28946.840 7994169.220 7.909552e+07
50256 Zimbabwe 2021.0 ZWE 15993525.0 NaN 0.531 0.033 8.396 0.547 6.962 ... 26225.256 11000.00 3.834786e+05 590070.590 2696.200 8000.00 425303.698 29313.660 112519.364 1.773784e+07
50257 Zimbabwe 2022.0 ZWE 16320539.0 NaN 0.531 0.033 8.856 0.460 5.477 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
50258 rows × 102 columns
merged_df.columns
Index(['country', 'year', 'iso_code', 'population', 'gdp', 'cement_co2',
'cement_co2_per_capita', 'co2', 'co2_growth_abs', 'co2_growth_prct',
...
'Peas, dry Production ( tonnes)', 'Palm oil Production (tonnes)',
'Grapes Production (tonnes)', 'Coffee, green Production ( tonnes)',
'Cocoa beans Production (tonnes)', 'Meat, chicken Production (tonnes)',
'Bananas Production ( tonnes)', 'Avocados Production (tonnes)',
'Apples Production (tonnes)', 'Gesamtproduktion (tonnes)'],
dtype='object', length=102)
continent = pd.read_csv("/kaggle/input/country-mapping-iso-continent-region/continents2.csv")
continent.columns
Index(['name', 'alpha-2', 'alpha-3', 'country-code', 'iso_3166-2', 'region',
'sub-region', 'intermediate-region', 'region-code', 'sub-region-code',
'intermediate-region-code'],
dtype='object')
continent = continent[['name', 'region']]
merged_df = pd.merge(merged_df, continent, left_on='country', right_on='name', how='left')
merged_df.rename(columns={'region': 'continent'}, inplace=True)
merged_df.columns
Index(['country', 'year', 'iso_code', 'population', 'gdp', 'cement_co2',
'cement_co2_per_capita', 'co2', 'co2_growth_abs', 'co2_growth_prct',
...
'Grapes Production (tonnes)', 'Coffee, green Production ( tonnes)',
'Cocoa beans Production (tonnes)', 'Meat, chicken Production (tonnes)',
'Bananas Production ( tonnes)', 'Avocados Production (tonnes)',
'Apples Production (tonnes)', 'Gesamtproduktion (tonnes)', 'name',
'continent'],
dtype='object', length=104)
import pandas as pd
import matplotlib.pyplot as plt
Angenommen, 'target_year' ist das Jahr, das du integrieren möchtest
target_year = 2023
Deine Daten und Visualisierung wie bisher
continent_totals = merged_df.groupby('continent').sum().reset_index()
food_columns = [col for col in merged_df.columns if 'tonnes' in col]
continent_marker = {
'Americas': 'o',
'Europe': '^',
'Africa': 's',
'Asia': 'D',
'Oceania': 'd',
}
plt.figure(figsize=(12, 8))
for food_col, color in zip(food_columns, ['black', 'gray', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'cyan', 'magenta', 'olive', 'lightblue', 'lightgreen', 'lightgray', 'darkred', 'darkblue', 'darkgreen']):
for country in continent_totals['continent']:
subset = continent_totals[continent_totals['continent'] == country]
marker = continent_marker.get(country)
plt.scatter(subset['co2'], subset[food_col], label=food_col,
marker=marker, color=color, s=100, alpha=0.5)
plt.title(f'Zusammenhang zwischen CO2-Emissionen und Lebensmittelproduktion nach Kontinent ({target_year})')
plt.xlabel('Gesamt-CO2-Emissionen (tonnes)')
plt.ylabel('Gesamtproduktion von Lebensmitteln (tonnes)')
legend_handles_country = [plt.Line2D([0], [0], marker=marker, color='w', label=country,
markersize=10, linestyle='None', markerfacecolor='black')
for country, marker in continent_marker.items()]
legend_handles_food = [plt.Line2D([0], [0], marker='o', color=color, label=food_col,
markersize=10, linestyle='None')
for food_col, color in zip(food_columns, ['black', 'gray', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'cyan', 'magenta', 'olive', 'lightblue', 'lightgreen', 'lightgray', 'darkred', 'darkblue', 'darkgreen'])]
plt.legend(handles=legend_handles_country + legend_handles_food,
title='Legende', loc='upper left', bbox_to_anchor=(1, 1))
plt.show()
import pandas as pd
import plotly.express as px
def create_food_production_chart(merged_df, target_year, continent):
# Filtern des DataFrames nach Jahr und Kontinent
filtered_df = merged_df[(merged_df['year'] == target_year) & (merged_df['continent'] == continent)]
# Auswahl der Spalten, die '(tonnes)' im Namen enthalten
tonnes_columns = [col for col in filtered_df.columns
if '(tonnes)' in col and col != 'Gesamtproduktion (tonnes)']
# Extrahieren der relevanten Daten
production_df = filtered_df[tonnes_columns]
# Umbenennen der Spalten, um '(tonnes)' zu entfernen
production_df.columns = [col.replace(" (tonnes)", "") for col in production_df.columns]
production_df.columns = [col.replace(" Production", "") for col in production_df.columns]
# Summieren der Werte nach neuem Spaltennamen "Item"
production_summary = production_df.sum().reset_index()
production_summary.columns = ["Item", "Production"]
# Visualisierung mit plotly express
fig = px.bar_polar(production_summary, r="Production", theta="Item",
template="plotly_dark", color="Item",
color_discrete_sequence=px.colors.sequential.Plasma_r)
# Layout anpassen (Titel setzen)
fig.update_layout(title=f"Lebensmittelproduktion in {continent} ({target_year})")
# Anzeigen der Plotly-Figur
fig.show()
create_food_production_chart(merged_df, 2019, "Oceania") erstelle weitere visualisierungen die sinnvoll wären