I am attempting to construct a pie chart of the weighting of certain sectors in a index.
given this sample data frame.
Data = { 'Company': ['Google', 'Nike', 'Goldman', 'Tesla'], 'Ticker': ['GOOG', 'NKE', 'GGG', 'TSA'], 'Sector': ['Tech', 'Consumer', 'Financial', 'Auto'], 'Weighting': ['10', '20', '40', '30']
df = pd.DataFrame(Data)
I have tried to create a dictionary of weights by sector.
Weight_Sector = df.groupby('Sector')['Weighting'].apply(list).to_dict()
I checked the dictionary and all was good. However when I went on to plot the pie chart with the following code, I get an error 'ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (9,) inhomogeneous part.
plt.pie(Weight_Sector.values(), startangle=90, autopct='%.1f%%')
In my actual data frame and dictionary there is a lot more values and most dictionary sectors have more than 1 item as a weight in them corresponding to different companies.
Any help is appreciated!!
CodePudding user response:
There are two problems:
- the values need to be numeric, now they are strings
- the individual values shouldn't be put into lists, they need to be just one number, e.g. taking the sum of all values belonging to the same category
As the original data only has one entry per category, the following example adds an extra 'Tech'.
from matplotlib import pyplot as plt
import pandas as pd
Data = {'Company': ['Microsoft', 'Google', 'Nike', 'Goldman', 'Tesla'], 'Ticker': ['MSFT', 'GOOG', 'NKE', 'GGG', 'TSA'],
'Sector': ['Tech', 'Tech', 'Consumer', 'Financial', 'Auto'], 'Weighting': ['1', '10', '20', '40', '30']}
df = pd.DataFrame(Data)
df['Weighting'] = df['Weighting'].astype(float) # make numeric
Weight_Sector = df.groupby('Sector')['Weighting'].sum().to_dict()
plt.pie(Weight_Sector.values(), labels=Weight_Sector.keys(),
startangle=90, autopct='%.1f%%', colors=plt.cm.Set2.colors)
plt.show()