Visualization
import pandas as pd
df = pd.DataFrame.from_csv("GroupedSP500.csv", encoding="UTF-8")
print(df.groupby("Group")["GICS Sector"].value_counts())
Let’s get the mean for our data.
print(df.groupby("Group").mean())
Let’s get rid of CIK column and then also plot a bar graph of all the mean data. We can use plot() to plot a pandas dataframe, and also can use the argument kind to decide what kind of plot.
import matplotlib.pyplot as plt
del df["CIK"]
df.groupby("Group").mean().plot(kind='bar')
plt.show()
Let’s also see about what the bar graph looks like without the index betas.
df[["Gold","NaturalGas","Oil","Group"]].groupby("Group").mean().plot(kind='bar')
plt.show()
Finally, we can get a pie graph as so….
for x in df["GICS Sector"].unique():
frame = df[df["GICS Sector"]==x]
frame["Group"].value_counts().plot(kind='pie',legend=True,title=x)
plt.show()
Source Code