<jupyter_start><jupyter_text>Quick plotting using pandas<jupyter_code>import pandas as pd
# Read in our data
surveys_df = pd.read_csv('../data/raw/surveys.csv')
# Checking your current working directory
import os
os.getcwd()
# Get the count for each species
species_counts = surveys_df.groupby('species_id')['record_id'].count()
species_counts
species_counts.plot(kind='bar')
# Count the number animals captured at each site
site_count = surveys_df.groupby('plot_id')['record_id'].count()
site_count.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>1. Average weight plotCreate a bar plot of average weight across all species per site.<jupyter_code>average_weight = surveys_df.groupby('plot_id')['weight'].mean()
average_weight.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>2. Sex plotCreate a bar plot of total males versus total females for the entire dataset.<jupyter_code>sex_totals = surveys_df.groupby('sex')['record_id'].count()
sex_totals.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>3. Summary challengeCreate a stacked bar plot, with weight on the Y axis, and the stacked variable being sex. The plot should show total weight by sex for each site.<jupyter_code>total_weights_by_sex_and_site = surveys_df.groupby(['plot_id', 'sex'])['weight'].sum()
total_weights_by_sex_and_site.head(4)
total_weights_by_sex_and_site.unstack(level=0) # alternative way of unstacking (that we won't use)
unstacked = total_weights_by_sex_and_site.unstack()
unstacked.head(4)
plt = unstacked.plot(kind='bar', stacked=True, title="Total weight by site and sex")
plt.set_ylabel("Weight")
plt.set_xlabel("Plot")
total_weights_by_sex_and_site.plot(kind='bar', stacked=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11863425925925926, 'fraction_numerical': 0.003472222222222222, 'mean_word_length': 8.5, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '573054', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 598, 'n_tokens_neox': 578, 'n_words': 132, 'path': 'files/notebooks/02-quick-plotting.ipynb', 'repo_name': 'escience-academy/2021-04-19-dc-python', 'size': '1728'}