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

Youtube Video commentary is also available.

problem

P-019: 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. If the sales amount (amount) is the same, the same ranking shall be given.

answer

code


df_amount_rank = pd.concat([df_receipt[['customer_id', 'amount']] \
,df_receipt['amount'].rank(method='min', 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
72747 ZZ000000000000 5480 4
10320 ZZ000000000000 5480 4
97294 CS021515000089 5440 7
28304 ZZ000000000000 5440 7
92246 CS009415000038 5280 9
68553 CS040415000200 5280 9

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. It is based on the verb concatenate (= concatenate). -'Rank (method (average / min / max / first),'ascending = True / False')' is a function that displays the ranking of the specified column. -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
[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) 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)"
Data science 100 knock (structured data processing) environment construction (Windows10)
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) # 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) # 4]
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 (P081 ~ 100)
100 Language Processing Knock Chapter 1 (Python)
Data Science Cheat Sheet (Python)
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock with Python (Chapter 3)
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
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)
Image processing 100 knock Q.6. Color reduction processing explanation
Python inexperienced person tries to knock 100 language processing 14-16
python image processing
Data analysis python
100 Language Processing Knock (2020): 28
Learn data science
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
100 language processing knock 00 ~ 02
Image processing by Python 100 knock # 11 smoothing filter (average filter)
[Data science memorandum] Handling of missing values ​​[python]
Python inexperienced person tries to knock 100 language processing 05-06
[python] Read data