• 30 天 Pandas 挑战 Day16:reset_index()将结果从 Series转为DataFrame


    题目:1741. 查找每个员工花费的总时间

    输入

    1. Employees table:
    2. +--------+------------+---------+----------+
    3. | emp_id | event_day | in_time | out_time |
    4. +--------+------------+---------+----------+
    5. | 1 | 2020-11-28 | 4 | 32 |
    6. | 1 | 2020-11-28 | 55 | 200 |
    7. | 1 | 2020-12-03 | 1 | 42 |
    8. | 2 | 2020-11-28 | 3 | 33 |
    9. | 2 | 2020-12-09 | 47 | 74 |
    10. +--------+------------+---------+----------+

    预期结果

    1. +------------+--------+------------+
    2. | day | emp_id | total_time |
    3. +------------+--------+------------+
    4. | 2020-11-28 | 1 | 173 |
    5. | 2020-11-28 | 2 | 30 |
    6. | 2020-12-03 | 1 | 41 |
    7. | 2020-12-09 | 2 | 27 |
    8. +------------+--------+------------+

    reset_index()时

    代码

    1. import pandas as pd
    2. def total_time(employees: pd.DataFrame) -> pd.DataFrame:
    3. employees['inter_time'] = employees['out_time'] - employees['in_time']
    4. employees = employees.groupby(['emp_id', 'event_day'])['inter_time'].sum()
    5. print(employees)
    6. print(type(employees))

    输出

    1. emp_id event_day
    2. 1 2020-11-28 173
    3. 2020-12-03 41
    4. 2 2020-11-28 30
    5. 2020-12-09 27
    6. Name: inter_time, dtype: Int64
    7. <class 'pandas.core.series.Series'>

    有reset_index()时

    代码

    1. import pandas as pd
    2. def total_time(employees: pd.DataFrame) -> pd.DataFrame:
    3. employees['inter_time'] = employees['out_time'] - employees['in_time']
    4. employees = employees.groupby(['emp_id', 'event_day'])['inter_time'].sum().reset_index()
    5. print(employees)
    6. print(type(employees))

    输出

    1. emp_id event_day inter_time
    2. 0 1 2020-11-28 173
    3. 1 1 2020-12-03 41
    4. 2 2 2020-11-28 30
    5. 3 2 2020-12-09 27
    6. <class 'pandas.core.frame.DataFrame'>

    reset_index()作用

    reset_index() 是 pandas 库中的一个方法,它的主要作用是对索引进行重置。当你使用 groupby 方法对数据进行聚合后,你会得到一个 Series,这个 Series 的索引是组合的列名(在你的例子中是 'emp_id' 和 'event_day',多重索引)。reset_index() 方法将这个索引重置为默认的整数索引,并且如果需要的话,它还可以创建一个新的列来保存原来的索引。
    在你的例子中,reset_index() 将根据 'emp_id' 和 'event_day' 的值为新的 DataFrame 创建索引,而原本的数值则作为新的 DataFrame 的一列,这就是为什么 reset_index() 能够将 Series 转化为 DataFrame 的原因。
    因此,reset_index() 方法是一个非常实用的工具,可以帮助你在进行数据聚合操作后,将结果从 Series 格式转化为更易于分析和使用的 DataFrame 格式。

    解题代码

    1. import pandas as pd
    2. def total_time(employees: pd.DataFrame) -> pd.DataFrame:
    3. employees['inter_time'] = employees['out_time'] - employees['in_time']
    4. employees: pd.DataFrame = employees.groupby(['emp_id', 'event_day'])['inter_time'].sum().reset_index()
    5. employees.rename(columns={'event_day': 'day', 'inter_time': 'total_time'}, inplace=True)
    6. return employees[['day', 'emp_id', 'total_time']]

    rename函数同时给多列rename时记得colunms=不能省略,参数名传递,不能用位置传递。

  • 相关阅读:
    酷宇宙观点:万物金融化,定义下一个金融服务时代
    面试金典--面试题 17.19. 消失的两个数字
    k8s查看pod日志的几种方法
    Mybatis学习笔记8 查询返回专题
    Linux内核之内存管理分段机制原理与实现(从无到有的过程)
    剑指 Offer II 048. 序列化与反序列化二叉树
    Redis事务、pub/sub、PipeLine-管道、benchmark性能测试详解
    【1++的C++进阶】之特殊类设计
    新闻管理系统设计与实现
    Linux基础指令(三)
  • 原文地址:https://blog.csdn.net/ciky2011/article/details/133707347