数据统计与数据分组18-25题(30 天 Pandas 挑战)

数据统计与数据分组

  • [1. 知识点](#1. 知识点)
    • [1.18 分箱与统计个数](#1.18 分箱与统计个数)
    • [1.19 分组与求和统计](#1.19 分组与求和统计)
    • [1.20 分组获取最小值](#1.20 分组获取最小值)
    • [1.21 分组获取值个数](#1.21 分组获取值个数)
    • [1.22 分组与条件查询](#1.22 分组与条件查询)
    • [1.23 分组与条件查询及获取最大值](#1.23 分组与条件查询及获取最大值)
    • [1.24 分组及自定义函数](#1.24 分组及自定义函数)
    • [1.25 分组+lambda函数统计](#1.25 分组+lambda函数统计)
  • [2. 题目](#2. 题目)
    • [2.18 按分类统计薪水(数据统计)](#2.18 按分类统计薪水(数据统计))
    • [2.19 查找每个员工花费的总时间(数据分组)](#2.19 查找每个员工花费的总时间(数据分组))
    • [2.20 游戏玩法分析 I(数据分组)](#2.20 游戏玩法分析 I(数据分组))
    • [2.21 每位教师所教授的科目种类的数量(数据分组)](#2.21 每位教师所教授的科目种类的数量(数据分组))
    • [2.22 超过5名学生的课(数据分组)](#2.22 超过5名学生的课(数据分组))
    • [2.23 订单最多的客户(数据分组)](#2.23 订单最多的客户(数据分组))
    • [2.24 按日期分组销售产品(数据分组)](#2.24 按日期分组销售产品(数据分组))
    • [2.25 每天的领导和合伙人(数据分组)](#2.25 每天的领导和合伙人(数据分组))

1. 知识点

1.18 分箱与统计个数

  • 分箱操作

    python 复制代码
    # float('inf')正无穷
    bins=[0,20000,50001,float('inf')]
    labels=['Low Salary', 'Average Salary', 'High Salary']
    accounts['category']=pd.cut(accounts['income'],bins=bins,labels=labels,right=False)
  • 数值统计

    python 复制代码
    accounts_new=accounts['category'].value_counts().reset_index()

1.19 分组与求和统计

  • 分组统计

    python 复制代码
    employees=employees.groupby(['event_day','emp_id']).agg({'total_time':'sum'}).reset_index()
  • 多种数值统计

    python 复制代码
    employees=employees.groupby(['event_day','emp_id']).agg(
    total_add=('total_time','sum'),
    total_mean=('total_time','mean')
    ).reset_index()

1.20 分组获取最小值

  • 分组获取最小值

    python 复制代码
    activity=activity.groupby('player_id').agg(first_login=('event_date','min')).reset_index()

1.21 分组获取值个数

  • 分组获取最小值

    python 复制代码
    teacher=teacher.groupby('teacher_id').agg(cnt=('subject_id','count')).reset_index()

1.22 分组与条件查询

python 复制代码
courses=courses.groupby('class').agg(counts=('student','count')).reset_index() # 分组
courses=courses.query('`counts`>5')[['class']] # 条件查询

1.23 分组与条件查询及获取最大值

  • 分组统计

    python 复制代码
    orders=orders.groupby('customer_number').agg(counts=('order_number','count')).reset_index()
  • 最大值

    python 复制代码
    max_orders=orders['counts'].max()
  • 条件查询

    python 复制代码
    results=orders.query(f'`counts`=={max_orders}')[['customer_number']]

1.24 分组及自定义函数

python 复制代码
def get_join(x):
        x_list=sorted(list(set(x)))
        return ','.join(x_list)
    def get_count(x):
        x_list=set(x)
        return len(x_list)
    activities=activities.groupby(['sell_date']).agg(num_sold=('product',get_count),products=('product',get_join)).reset_index()

1.25 分组+lambda函数统计

python 复制代码
daily_sales=daily_sales.groupby(['date_id','make_name']).agg(
        unique_leads=('lead_id',lambda x:len(set(x))),
        unique_partners=('partner_id',lambda x:len(set(x)))
        ).reset_index()

2. 题目

2.18 按分类统计薪水(数据统计)


python 复制代码
import pandas as pd

def count_salary_categories(accounts: pd.DataFrame) -> pd.DataFrame:
    # float('inf')
    bins=[0,20000,50001,float('inf')]
    labels=['Low Salary', 'Average Salary', 'High Salary']
    accounts['category']=pd.cut(accounts['income'],bins=bins,labels=labels,right=False)
    accounts_new=accounts['category'].value_counts().reset_index()
    accounts_new=accounts_new.rename(columns={'count':'accounts_count'})
    accounts_new=accounts_new.sort_values('category',ascending=False)
    return accounts_new

2.19 查找每个员工花费的总时间(数据分组)


python 复制代码
import pandas as pd

def total_time(employees: pd.DataFrame) -> pd.DataFrame:
	# pandas流
    employees=employees.assign(total_time = employees.out_time-employees.in_time).groupby(['event_day','emp_id']).agg({'total_time':'sum'}).reset_index().rename(columns = {'event_day':'day'})
    # employees['total_time']=employees['out_time']-employees['in_time']
    # employees=employees.groupby(['event_day','emp_id'])['total_time'].sum().reset_index()
    # employees=employees.rename(columns={'event_day':'day'})
    # employees.sort_values('emp_id',inplace=True)
    return employees

2.20 游戏玩法分析 I(数据分组)


python 复制代码
import pandas as pd

def game_analysis(activity: pd.DataFrame) -> pd.DataFrame:
    activity=activity.groupby('player_id').agg(first_login=('event_date','min')).reset_index()
    return activity

2.21 每位教师所教授的科目种类的数量(数据分组)


python 复制代码
import pandas as pd

def count_unique_subjects(teacher: pd.DataFrame) -> pd.DataFrame:
    teacher.drop_duplicates(['teacher_id','subject_id'],inplace=True)
    teacher=teacher.groupby('teacher_id').agg(cnt=('subject_id','count')).reset_index()
    return teacher

2.22 超过5名学生的课(数据分组)



python 复制代码
import pandas as pd

def find_classes(courses: pd.DataFrame) -> pd.DataFrame:
    courses=courses.groupby('class').agg(counts=('student','count')).reset_index()
    return courses.query('`counts`>5')[['class']]

2.23 订单最多的客户(数据分组)


python 复制代码
import pandas as pd

def largest_orders(orders: pd.DataFrame) -> pd.DataFrame:
    orders=orders.groupby('customer_number').agg(counts=('order_number','count')).reset_index()
    max_orders=orders['counts'].max()
    results=orders.query(f'`counts`=={max_orders}')[['customer_number']]
    return results

2.24 按日期分组销售产品(数据分组)


python 复制代码
import pandas as pd

def categorize_products(activities: pd.DataFrame) -> pd.DataFrame:
    activities=activities.groupby(['sell_date']).agg(
        num_sold=('product',lambda x:len(set(x))),
        products=('product',lambda x:','.join(sorted(list(set(x)))))
        ).reset_index()
    return activities
    

2.25 每天的领导和合伙人(数据分组)



python 复制代码
import pandas as pd

def daily_leads_and_partners(daily_sales: pd.DataFrame) -> pd.DataFrame:
    daily_sales=daily_sales.groupby(['date_id','make_name']).agg(
        unique_leads=('lead_id',lambda x:len(set(x))),
        unique_partners=('partner_id',lambda x:len(set(x)))
        ).reset_index()
    return daily_sales
相关推荐
benchmark_cc11 小时前
A 股、港股、美股如何建立统一的标的主数据格式?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc15 小时前
用 00700.HK 做港股行情清洗:字段核对、校验与个人量化开发实践
python·数据分析·pandas·量化交易·股票数据·quantdash
第九序言T17 小时前
数据分析岗 Pandas 面试题 20 道(上):概念与读代码
python·数据分析·pandas·数据处理
2601_9669496518 小时前
如何封装 qd.klines.batch(),让批量 K 线查询函数在多个策略中复用?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
Blockbuater_drug1 天前
《经典机器学习实战》第 2 课:回归入门——环境、工具与数据准备
线性回归·pandas·scikit-learn·数据可视化·数据清洗·最小二乘法·conda环境
烂蜻蜓2 天前
Pandas入门教程(十六):常用函数速查手册——读写、清洗、选择、排序、分组与统计
pandas
quantdash_cc2 天前
个人开发者用 QuantDash 读取港股行情:00700.HK 的 Python 函数封装与校验实践
python·数据分析·pandas·量化交易·股票数据·quantdash
databook3 天前
Pandas 与 Polars:语法、性能和资源占用方面的对比
python·数据分析·pandas
桑年3 天前
四、Pandas数据处理
人工智能·python·线性代数·pandas·数据可视化·数据处理
第九序言T3 天前
Python 数据处理进阶(二十四)把脚本变成工具:argparse、logging 与定时任务
python·自动化·pandas