current position：Home>This pandas exercise must be successfully won
This pandas exercise must be successfully won
20220129 12:50:55 【PI dada】
official account ： Youer cottage
author ：Peter
edit ：Peter
Hello everyone , I am a Peter~
Wrote a lot Pandas The article , It mainly explains the usage of common operations and functions . Today, I made a fruit order and sales data （ Analog data , Just for learning ）, It is mainly used to deepen the understanding of how to use flexibly and quickly Pandas To fulfill our needs .
Pandas article
recommend
Data interpretation
1、 The first data of the simulation are 5 A field ： The order number 、 Next single 、 goods 、 Price 、 Number
 The order number ： The order number of each order , One or more items exist in an order number
 Next single ： A person may go down 1 One or more orders , For example, Zhang San only placed an order , Li Si placed several orders
 goods ： The same item may appear in multiple orders
 Price ： The price of each item in each order , In different orders , The price of the same commodity may be different , such as SOD Apple in the order is 10, But in DFH In the order is 9.8
 Number ： Sales quantity of each item in each order
2、 There are only two fields in the second data of the simulation ： Commodity and origin
At the same time we can see ： There are differences between the two data sheet Medium , Storage becomes xlslx file , And there is no missing value data .
demand 1： Read data in different ways
There is the same Excel Different in sheet in , We take different ways to read ：
The way 1： Specify both files and sheet The name of
import pandas as pd # Import the package first
Copy code
The way 2： Specify a file name and sheet The index number of , Index from 0 Start
demand 2： The combination of the two data
You can see two sheet The data in is through “ goods ” This field is associated with , We use pandas Medium merge function , And keep the first （ On the left left） All the information in the table .
merge Function is a very important function , Can handle flexibly Pandas Data merging in .
The following requirements are processed for the data merged above
demand 3： Order quantity 、 Number of customers 、 Commodity volume
Order quantity ： How many orders have been placed in total
unique： Chinese has a unique meaning , The field of order number has several unique characters 、 The only information . The total is 7 Order per order
Same thing ： How many order users can you get 、 How many kinds of goods are sold ？
demand 4： Order quantity per user
Is to ask how many orders each user has placed ： Use groupby Group and count the order quantity of each issuer .
 First use groupby Function to group
 Then use the aggregate function nunique, Count each one “ The order number ” The number of （ To heavy statistics ）
 Finally, reset the index
I saw Li four times 3 Zhang order , The most.
demand 5： Total consumption amount per user
1、 Add a column first ： Total
2、 Two different ways of grouping and regrouping
demand 6： Orders from different places of origin 、 sales 、 Total sales
demand 7： The item with the highest price in each order
Find the item with the highest price in each order , such as ：SOD The highest price in the order is grapes
The way 1： The first implementation is as follows ：
 First arrange the whole in descending order
 Then group according to the order number , Take out the first first Data is enough
The way 2： The implementation is as follows
1、 First, each order number is arranged in descending order according to the price
2、 Mixed use of multiple functions , You can run it separately to see the results of each step
df.groupby(" The order number ").apply(lambda x: x.sort_values(" Price ",ascending=False)).reset_index(drop=True).groupby(" The order number ").first().reset_index()
Copy code
The way 3： Use... When grouping groupby_keys Parameters
demand 8： The highest price in each order 2 position
Take out the highest price in each order 2 position , If there is only one, take out one .
The above is the highest data after taking out the grouping , Article 1 first. In this requirement, we use head function , You can take out any n Data ：TopN
demand 9： The unit price of each commodity （ Retain 2 Decimal place ）
Let's disassemble the meaning of the title ：
 Every product ： It is determined that the grouped elements are groupby=" goods "
 Unit price ： First find the total sales of each commodity , I'm looking for the number of orders for each commodity , Final division
How to keep two decimal places for the unit price of the above commodity pen ？ Two ways to achieve ：
copyright notice
author[PI dada],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291250506791.html
The sidebar is recommended
 [Python introduction project] use Python to generate QR code
 Compile D + +, and use d to call C from python
 Quickly build Django blog based on function calculation
 Python collects and monitors system data  psutil
 Finally, this Python import guide has been sorted out. Look!
 Quickly build Django blog based on function calculation
 Python interface test unittest usage details
 Implementation of toplevel design pattern in Python
 You can easily get started with Excel. Python data analysis package pandas (VII): breakdown
 Python simulation random coin toss (non optimized version)
guess what you like

Python tiktok 5000+ V, and found that everyone love this video.

Using linear systems in python with scipy.linalg

Using linear systems in python with scipy.linalg

Together with Python to do a license plate automatic recognition system, fun and practical!

You can easily get started with Excel. Python data analysis package pandas (XI): segment matching

Advanced practical case: Javascript confusion of Python anti crawling

Using linear systems in python with scipy.linalg

Fast power modulus Python implementation of large numbers

Quickly build Django blog based on function calculation

This paper clarifies the chaotic switching operation and elegant derivation of Python
Random recommended
 You can easily get started with Excel pandas (I): filtering function
 You can easily get started with Excel. Python data analysis package pandas (II): advanced filtering (I)
 You can easily get started with Excel. Python data analysis package pandas (2): advanced filtering (2)
 You can easily get started with Excel. Python data analysis package pandas (3): making score bar
 Test Development: self study Dubbo + Python experience summary and sharing
 You can easily get started with Excel. Python data analysis package pandas (V): duplicate value processing
 How does Python correctly call jar package encryption to get the encrypted value?
 Python 3 interview question: give an array. If there is 0 in the array, add a 0 after 0, and the overall array length remains the same
 Python simple Snake game (single player mode)
 Using linear systems in python with scipy.linalg
 Python executes functions and even code through strings! Come and understand the operation of such a top!
 Decoding the verification code of Taobao slider with Python + selenium, the road of information security
 [Python introduction project] use Python to generate QR code
 Vanessa basks in her photos and gets caught up in the golden python. There are highlights in the accompanying text. She can't forget Kobe after all
 [windows] Python installation pyteseract
 [introduction to Python project] create bar chart animation in Python
 Fundamentals of Python I
 Python series tutorials 116
 Python code reading (chapter 35): fully (deeply) expand nested lists
 Practical series 1 ️⃣ Wechat applet automatic testing practice (with Python source code)
 Python Basics: do you know how to use lists?
 Solution of no Python 3.9 installation was detected when uninstalling Python
 [Python homework] coupling network information dissemination
 [common links of Python & Python]
 Python application software development tool  tkinterdesigner v1.0 5.1 release!
 [Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger
 [Python development tool Tkinter designer]: Lecture 2: introduction to Tkinter designer's example project
 [Python development tool Tkinter designer]: Lecture 1: introduction to the basic functions of Tkinter Designer
 [introduction to Python tutorial] use Python 3 to teach you how to extract any HTML main content
 Python socket implements UDP server and client
 Python socket implements TCP server and client
 leetcode 1261. Find Elements in a Contaminated Binary Tree（python）
 [algorithm learning] 1486 Array XOR operation (Java / C / C + + / Python / go / trust)
 leetcode 1974. Minimum Time to Type Word Using Special Typewriter（python）
 The mobile phone uses Python to operate picture files
 [learning notes] Python exception handling try except...
 Two methods of using pandas to read poorly structured excel. You're welcome to take them away
 Python sum (): the summation method of Python
 Practical experience sharing: use pyo3 to build your Python module
 Using Python to realize multitasking process