I apologize in advance for my inability to explain what I am trying to do into words. I am very confused. I have a data set where I have corporate environmental impact data and I have created a column with their impact category - if they have a positive value in the total environmental impact column, their category is 'positive', and if they have negative value, their category is 'negative'. I am looking at companies by country. I have no issues with this if the country has companies in both categories, but if a country does not have any positive countries, I have a problem making the graph I am trying to make.
Here is the working code for a country which has both positive and negative categories:
usa_company_impcat = pd.crosstab(usa_company_filtered['Year'], usa_company_filtered['Impact_Category'])
usa_company_impcat['Total_Count'] = usa_company_impcat.loc[:,['Negative', 'Positive']].sum(axis = 1) # adding a total column
usa_company_impcat = usa_company_impcat.rename_axis("Year").reset_index() # fixing the year column
usa_company_impcat
Here is the output I get
If I try to do this with a country who only has rows that are negative, I get this error:
KeyError: "['Positive'] not in index"
Is there a simple way to fix this? Should I just give up on those countries?
SOLVED: I figured out the simplest way to achieve what I wanted was to add a 'Positive' column populated with zeros. I apologize that my original question wasn't better worded. Here is the fixed code for one of the countries who had no rows possessing 'Positive'.
ROK_ind_impcat = pd.crosstab(ROK_industry['Year'], ROK_industry['Impact_Category'])
ROK_ind_impcat['Positive'] = 0 # this is the line I added to replicate a Positive column for my later graph
ROK_ind_impcat['Total_Count'] = ROK_ind_impcat.loc[:,['Negative', 'Positive']].sum(axis = 1)
ROK_ind_impcat = ROK_ind_impcat.rename_axis("Year").reset_index() # fixing the year column
ROK_ind_impcat