Skip to content
Advertisement

How can I print a statement in Python to show the results of a mathematical operation on a Pandas Dataframe?

So I’ve got a simple summary dataframe of sales by gender that goes:

Gender |    Sales
___________________
M      |    25
F      |    30

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:

m_sales = df.loc[df['Gender'] == 'M']
f_sales = df.loc[df['Gender'] == 'F']

print('The mean gap in the amount sold is:', m_sales['Sales'] - f_sales['Sales'] / m_sales['Sales'] * 100, '%')

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:

m_sales = df.loc[df['Gender'] == 'M']["Sales"].iloc[0]
f_sales = df.loc[df['Gender'] == 'F']["Sales"].iloc[0]

print('The mean gap in the amount sold is:', (f_sales - m_sales) / f_sales * 100, '%')
The mean gap in the amount sold is: 16.666666666666664 %

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.

print(f'The mean gap in the amount sold is: {(f_sales - m_sales) / f_sales * 100:.2f}%')
The mean gap in the amount sold is: 16.67%
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement