[Python] Data Science 100 Knock (Structured Data Processing) 020 Explanation

Youtube Video commentary is also available.

problem

P-020: Give ranks to the receipt details data frame (df_receipt) in descending order of sales amount (amount) per item, and extract the first 10 items. Items should display customer ID (customer_id), sales amount (amount), and assigned rank. In addition, even if the sales amount (amount) is the same, give a different ranking.

answer

code


df_amount_rank = pd.concat([df_receipt[['customer_id', 'amount']] \
,df_receipt['amount'].rank(method='first', ascending=False)], axis=1)

df_amount_rank.columns = ['customer_id', 'amount', 'amount_ranking']

df_amount_rank.sort_values('amount_ranking', ascending=True).head(10)

output

customer_id amount amount_ranking
1202 CS011415000006 10925 1
62317 ZZ000000000000 6800 2
54095 CS028605000002 5780 3
4632 CS015515000034 5480 4
10320 ZZ000000000000 5480 5
72747 ZZ000000000000 5480 6
28304 ZZ000000000000 5440 7
97294 CS021515000089 5440 8
596 CS015515000083 5280 9
11275 CS017414000114 5280 10

Commentary

**-In Pandas DataFrame / Series, create a new rank column, concatenate the columns, and rank the data. -Use when you want to see numerical information in ranking format. -'Concat ('','','')' is a function that concatenates the specified columns. 'axis = 1'refers to concatenating columns. (*'Axis = 0'refers to the row direction and'axis = 1'refers to the column direction) -'Rank (method (average / min / max / first),'ascending = True / False')' is a function that displays the ranking of the specified column. ・ Average is the average value, min is aligned with the lower one, max is aligned with the higher one, and first is given the rank in the order of appearance. -In this case, the amount ranking is displayed by rank, and concat is used to concatenate with the specified column. -'Columns ('','','')' is a function to respecify column names. -Using sort_values used on pages 017 and 018, they are sorted and displayed in ranking order. ** **

Recommended Posts

"Data Science 100 Knock (Structured Data Processing)" Python-007 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-006 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-001 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-002 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 021 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-005 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-004 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 020 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 025 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-003 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 019 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 001-010 Impressions + Explanation Link Summary
[Python] 100 knocks on data science (structured data processing) 018 Explanation
[Python] 100 knocks on data science (structured data processing) 023 Explanation
[Python] 100 knocks on data science (structured data processing) 030 Explanation
[Python] 100 knocks on data science (structured data processing) 022 Explanation
[Python] 100 knocks on data science (structured data processing) 017 Explanation
[Python] 100 knocks on data science (structured data processing) 026 Explanation
[Python] 100 knocks on data science (structured data processing) 016 Explanation
[Python] 100 knocks on data science (structured data processing) 024 Explanation
[Python] 100 knocks on data science (structured data processing) 027 Explanation
[Python] 100 knocks on data science (structured data processing) 029 Explanation
[Python] 100 knocks on data science (structured data processing) 015 Explanation
[Python] 100 knocks on data science (structured data processing) 028 Explanation
Preparing to try "Data Science 100 Knock (Structured Data Processing)"
That's why I quit pandas [Data Science 100 Knock (Structured Data Processing) # 2]
That's why I quit pandas [Data Science 100 Knock (Structured Data Processing) # 1]
That's why I quit pandas [Data Science 100 Knock (Structured Data Processing) # 3]
That's why I quit pandas [Data Science 100 Knock (Structured Data Processing) # 5]
That's why I quit pandas [Data Science 100 Knock (Structured Data Processing) # 6]
100 Language Processing with Python Knock 2015
Data science 100 knock commentary (P021 ~ 040)
Data science 100 knock commentary (P061 ~ 080)
Data science 100 knock commentary (P041 ~ 060)
Data science 100 knock commentary (P081 ~ 100)
100 Language Processing Knock Chapter 1 (Python)
Data Science Cheat Sheet (Python)
100 Language Processing Knock Chapter 2 (Python)
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 Language Processing Knock with Python (Chapter 3)
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
100 Language Processing Knock Chapter 1 by Python
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
Image processing with Python 100 knock # 10 median filter
Image processing by Python 100 knock # 1 channel replacement
100 Language Processing Knock-91: Preparation of Analogy Data
I took Udemy's "Practical Python Data Science"
100 Language Processing Knock with Python (Chapter 2, Part 2)
Image processing with Python 100 knock # 12 motion filter
100 image processing by Python Knock # 6 Color reduction processing
[Python] Various data processing using Numpy arrays
100 Language Processing Knock with Python (Chapter 2, Part 1)
python image processing
Data analysis python
100 Language Processing Knock (2020): 28
Python file processing
100 language processing knock-20 (using pandas): reading JSON data
Python inexperienced person tries to knock 100 language processing 07-09
100 Language Processing Knock (2020): 38
Python inexperienced person tries to knock 100 language processing 10 ~ 13