【问题标题】:Insert Value to a Cell with result of group by pandas将值插入到单元格中,结果由 pandas 分组
【发布时间】:2021-12-17 00:29:22
【问题描述】:

我有一个很大的 excel 文件,像这样:
Table 1
我的愿望表是这样的:
my dsire Table
我使用分组、计数和求和,例如:

import pandas as pd
import openpyxl as op
from openpyxl import load_workbook
from openpyxl import Workbook
import numpy as np
path1 = r"users.xlsx"
data = pd.read_excel(path1, engine='openpyxl')
df = pd.DataFrame(data)
NumberOfChild = df.groupby('Parent ID')['Parent ID'].count().to_frame('Employees Number')
NumberOfBooking = df.groupby('Parent ID')['Reservations Count'].transform('sum')

这给了我正确的 Booking 和 Child 数量,但我不能在列 numberOfChild 和 numberOfBooking 中获得这些值

【问题讨论】:

标签: python python-3.x pandas pandas-groupby


【解决方案1】:

假设您有以下数据框

>>> df
  id  parent_id  reservations  
0   1        NaN             1 
1   2        1.0             3 
2   3        1.0             5
3   4        NaN             2 
4   5        4.0             6
5   6        NaN             7 

首先计算孩子的数量

>>> children = df.groupby("parent_id").id.count().rename("children")
>>> children
parent_id
1.0    2
4.0    1
Name: children, dtype: int64

然后创建一个聚合一个新列,如果该行没有 parent_id,则为 id,否则为 parent_id

>>> df["book_key"] = df.parent_id.fillna(df.id).astype(int)
>>> df
   id  parent_id  reservations  book_key
0   1        NaN             1         1
1   2        1.0             3         1
2   3        1.0             5         1
3   4        NaN             2         4
4   5        4.0             6         4
5   6        NaN             7         6

使用这个新键来计算预订总数

>>> reservations = df.groupby("book_key").reservations.sum().rename("total")
>>> reservations 
book_key
1    9
4    8
6    7
Name: total, dtype: int64

最后加入数据框,删除 book_key 列并可选地将 NaN 替换为 ""

>>> df = df.set_index("id").join(children).join(reservations).drop(columns="book_key").fillna("")
>>> df
   parent_id  reservations children total
id
1                        1      2.0   9.0
2        1.0             3
3        1.0             5
4                        2      1.0   8.0
5        4.0             6
6                        7            7.0

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多