So I’ve got a simple summary dataframe of sales by gender that goes:
JavaScript
x
5
1
Gender | Sales
2
___________________
3
M | 25
4
F | 30
5
All I want to do now is to return a line in Python that reads: The mean gap in the amount sold is 16.67%
This is simply 30 – 25 divided by 30, and multiplied by 100; and I want a % sign at the end.
I have tried:
JavaScript
1
5
1
m_sales = df.loc[df['Gender'] == 'M']
2
f_sales = df.loc[df['Gender'] == 'F']
3
4
print('The mean gap in the amount sold is:', m_sales['Sales'] - f_sales['Sales'] / m_sales['Sales'] * 100, '%')
5
Unfortunately this does not work. I get:
The mean gap in the amount sold is: 0 NaN 1 NaN Name: Sales, dtype: object %
Thoughts please? I am very much a beginner so sorry for such a basic query!
Advertisement
Answer
Append ["Sales"].iloc[0]
to your filter expressions to obtain directly values for M
and F
, and then project these changes to the print()
function, too:
JavaScript
1
5
1
m_sales = df.loc[df['Gender'] == 'M']["Sales"].iloc[0]
2
f_sales = df.loc[df['Gender'] == 'F']["Sales"].iloc[0]
3
4
print('The mean gap in the amount sold is:', (f_sales - m_sales) / f_sales * 100, '%')
5
JavaScript121The mean gap in the amount sold is: 16.666666666666664 %
2
The explanation:
df.loc[df['Gender'] == 'M']
is a dataframe;- selecting the
"Sales"
column by appending["Sales"]
you obtain a series (with only 1 element), and - by appending
.iloc[0]
you obtain the first (=the only one) element of that series.
Note:
You may use f-string (for Python 3.6+) or .format()
method for adjusting your output, e.g.
JavaScript
1
2
1
print(f'The mean gap in the amount sold is: {(f_sales - m_sales) / f_sales * 100:.2f}%')
2
JavaScript121The mean gap in the amount sold is: 16.67%
2