0

everyone. I tried this method Can I use a specific element inside the groupby functions instead of using column name?

but i gor error.

So I need to calculate the population mean next to the 'Hispanic or Latino label within this dataframe. How do I select exactly the population values for "Hispanic or Latino" and calculate the mean for them?

kind_of_pop_df = pd.DataFrame(data=pop_list_us, columns=["Year", "Ethnicity", "Hispanic Population"])
kind_of_pop_df.head(7)
kind_of_pop_df.set_index("Year",inplace=True)
print(kind_of_pop_df)

                   Ethnicity  Hispanic Population
Year                                             
2019  Not Hispanic or Latino             10710524
2019      Hispanic or Latino              1583156
2019  Not Hispanic or Latino              2763709
2019      Hispanic or Latino               320595
2019  Not Hispanic or Latino                42268
...                      ...                  ...
2013      Hispanic or Latino                 2791
2013  Not Hispanic or Latino               105872
2013      Hispanic or Latino              1567526
2013  Not Hispanic or Latino               323451
2013      Hispanic or Latino               241243

[98 rows x 2 columns]

Can you answer with specific answers? Thanks!

2 Answers2

0

it's the error

kind_of_pop_df.loc[kind_of_pop_df['Ethnicity'].eq('Hispanic or Latino')].groupby('Hispanic Population')[2019].sum()

KeyError: 'Column not found: 2019'
0

Is the Hispanic column blank? Anyways, what I would do was group by year, and aggregate the population by sum and then take the mean

  • Hi, I cannot post a reply to your answer due to less reputation points. But you need to kind_of_pop_df.loc[kind_of_pop_df['Ethnicity'].eq('Hispanic or Latino')].groupby('Hispanic Population')['year' == 2019].sum() – Mustafa Anandwala May 30 '22 at 16:06
  • No "Hispanic Population" is only one column. I want to sum only values of "Hispanic or Latino" and i don't know hot to get only these ones and not "Not Hispanic and latino" for year 2019! – Pietro Enea May 30 '22 at 16:07
  • I did this but i get this oneKeyError: 'Column not found: False' – Pietro Enea May 30 '22 at 16:09