text_html_output_1.png
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

meat = pd.read_csv('/kaggle/input/meat-consumption/meat_consumption.csv')
meat

meat.drop(['frequency', 'indicator'], axis=1, inplace=True)
meat

meat['KG_CAP values'] = meat['value']
conversion_factor = 1000
meat.loc[meat['measure'] == 'THND_TONNE', 'KG_CAP values'] *= conversion_factor
meat

meat['time'] = pd.to_datetime(meat['time'], format='%Y')
meat

grouped_data = meat.groupby('subject')['KG_CAP values'].agg(['sum', 'mean'])

# Create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 10))

# Plot the sum of KG_CAP values
colors = ['blue', 'green', 'orange', 'red']
ax1.bar(grouped_data.index, grouped_data['sum'], color=colors)
ax1.set_ylabel('Total mean consumed')
ax1.set_xticklabels(grouped_data.index, rotation=45, ha='right')

# Add exact values on top of each bar in the sum plot
for i, v in enumerate(grouped_data['sum']):
    ax1.text(i, v, str(v), ha='center', va='bottom')

# Plot the mean of KG_CAP values
ax2.bar(grouped_data.index, grouped_data['mean'], color=colors)
ax2.set_ylabel('Mean consumption of meat')
ax2.set_xticklabels(grouped_data.index, rotation=45, ha='right')

# Add exact values on top of each bar in the mean plot
for i, v in enumerate(grouped_data['mean']):
    ax2.text(i, v, str(v), ha='center', va='bottom')

# Set x-axis label and title for the entire figure
plt.xlabel('Type of meat')
plt.suptitle('Total and mean consumption of meat')

# Adjust spacing between subplots
plt.tight_layout()

# Display the plot
plt.show()

meat['time'] = pd.to_datetime(meat['time'], format='%Y')

subjects = ['BEEF', 'PIG', 'POULTRY', 'SHEEP']
filtered_data = meat[meat['subject'].isin(subjects)]

time_intervals = [
    (pd.Timestamp('1990-01-01'), pd.Timestamp('1997-01-01')),
    (pd.Timestamp('1997-01-01'), pd.Timestamp('2004-01-01')),
    (pd.Timestamp('2004-01-01'), pd.Timestamp('2010-01-01')),
    (pd.Timestamp('2010-01-01'), pd.Timestamp('2016-01-01')),
    (pd.Timestamp('2016-01-01'), pd.Timestamp('2022-01-01')),
    (pd.Timestamp('2022-01-01'), pd.Timestamp('2029-01-01'))
]

fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes = axes.flatten()

for i, subject in enumerate(subjects):
    ax = axes[i]
    ax.set_title(subject)
  
    subject_data = filtered_data[filtered_data['subject'] == subject]

    x_labels = []
    x_ticks = []
    for j, (start_date, end_date) in enumerate(time_intervals):
        interval_data = subject_data[(subject_data['time'] >= start_date) & (subject_data['time'] < end_date)]
        total_count = interval_data['KG_CAP values'].sum()
        
        ax.bar(j, total_count)
        
        x_label = f'{start_date.year}-{end_date.year}'
        x_labels.append(x_label)
        x_ticks.append(j)

    ax.set_xticks(x_ticks)
    ax.set_xticklabels(x_labels, rotation='vertical')

    ax.set_ylabel('Total Count of KG_CAP values')

plt.tight_layout()

plt.show()

grouped_data = filtered_data.groupby('location')['KG_CAP values'].mean()
top_locations = grouped_data.nlargest(15)
fig, ax = plt.subplots(figsize=(10, 8))
colors = plt.cm.Set3(range(len(top_locations)))
ax.bar(top_locations.index, top_locations.values, color=colors)
ax.set_xticklabels(top_locations.index, rotation=45, ha='right')
ax.set_xlabel('Location')
ax.set_ylabel('Mean KG_CAP values')
ax.set_title('Top 15 Locations with Highest Mean KG_CAP Values')
plt.tight_layout()
plt.show()