import numpy as np
import pandas as pd
import matplotlib
file = "U.S._natural_gas_production.csv"
gas_df = pd.read_csv(file, decimal=',')
gas_df.head(3)
gas_df.shape
gas_df.columns
gas_df.info()
gas_df["Month"] = pd.to_datetime(gas_df["Month"])
month_type = gas_df['Month'].dtype
gas_df.info()
gas_df['Month'].dtype
gas_df['Month'].dt.year.head()
gas_df['Month'].dt.month.tail()
yearly_gas_df = gas_df.groupby(gas_df["Month"].dt.year).sum()
yearly_gas_df
plot = yearly_gas_df.filter(items=['U.S.']).plot(kind="bar")
plot.set_xlabel("Year");
filtered_yearly_gas_df = yearly_gas_df[1:-1]
filtered_yearly_gas_df
plot = filtered_yearly_gas_df.filter(items=['U.S.']).plot(kind="bar")
plot.set_xlabel("Year");
filtered_yearly_gas_df.columns[1:].sort_values()
plot = filtered_yearly_gas_df.filter(items=['Colorado', 'Louisiana', 'Ohio', 'Utah']).plot()
plot.set_xlabel("Year");
file = "U.S._crude_oil_production.csv"
oil_df = pd.read_csv(file, decimal=',')
oil_df.head(3)
oil_df["Month"] = pd.to_datetime(oil_df["Month"])
month_type_oil = oil_df['Month'].dtype
yearly_oil_df = oil_df.groupby(oil_df["Month"].dt.year).sum()
yearly_oil_df
yearly_oil_df.columns = yearly_oil_df.columns.str.strip()
yearly_oil_df
yearly_oil_df.filter(items=['U.S. Crude Oil']).plot(kind='bar');
filtered_yearly_oil_df = yearly_oil_df[1:-1]
total_gas = filtered_yearly_gas_df.filter(items=['U.S.'])
total_gas.columns = [ 'Gas' ]
display(total_gas.head(3))
total_oil = filtered_yearly_oil_df.filter(items=['U.S. Crude Oil'])
total_oil.columns = [ 'Crude Oil' ]
total_oil.head(3)
merged_df = pd.concat([total_oil, total_gas], axis='columns')
merged_df
plot = merged_df.plot(kind="bar")
plot.set_xlabel("Year")
plot.legend(['Gas (Millions of Cubic feet)', 'Crude Oil (Thousands of barrels)']);