Costly Customers
Sometimes we don't receive as much as we spend. The same with some customers in our DataFrame. We do spend more money to 'buy' this customer, but we don't receive the the same amount of money.
Let's find the percentage of these customers!
We want to find costly customers, not payments. To do that, we need to add additional
customer_id.nunique()
that will find only unique customers with their costly (or not) payments.
To find the percentage, use the next formula:
Tarefa
Swipe to start coding
- Count the number of the costly customers:
- Set the condition if the value in the
'cost'
column is greater than the value in the'money_spent'
column - Use unique customers to find the metric
- Print the number of the costly customers.
- Find the percentage of costly customers using the formula.
- Print the result of countings in the
0.00
format.
Solução
99
1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/783d7288-e86b-4b89-9966-a2fe97995277/section_3_dataset_upd.csv')
# Count the number of the costly customers
customer_counter = df[df['cost'] > df['money_spent']].customer_id.nunique()
# Print the number of the costly customers
print('The number of costly customers', customer_counter)
# Find the percentage of costly customers
percentage_customer = (customer_counter * 100) / df.customer_id.nunique()
# Print the result in 0.00 format
print('The percentage of costly customers', round(percentage_customer, 2), '%')
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 4. Capítulo 6
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import pandas as pd
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/783d7288-e86b-4b89-9966-a2fe97995277/section_3_dataset_upd.csv')
# Count the number of the costly customers
customer_counter = df[df___ > ___['___']].customer_id.___()
# Print the number of the costly customers
___('The number of costly customers', customer_counter)
# Find the percentage of costly customers
percentage_customer = (___ * 100) / df.___.nunique()
# Print the result in 0.00 format
print('The percentage of costly customers', ___(___, 2), '%')
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo