You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
812 B
Python

# -*- coding: utf-8 -*-
# @Time : 2024/7/25 15:33
# @Author : zhaoxiangpeng
# @File : test.py
import pandas as pd
table = pd.DataFrame([
{"A": 1, "B": 2, "C": 3, "D": 2, "T": "X"},
{"A": 2, "B": 3, "C": 4, "D": 1, "T": "Y"},
{"A": 3, "B": 4, "C": 1, "D": 2, "T": "X"},
{"A": 4, "B": 1, "C": 2, "D": 3, "T": "Z"},
])
print(table)
print('-0'*50)
big_table = pd.DataFrame()
group_by = table.groupby(by=['T'])
for gn, group in group_by:
print('gn:', gn)
print(group)
a = group[["A", "B", "C", "D"]].sum()
print(a)
f = group[group['D'] == 2]
if f.empty:
first = group.head(1)
else:
first = f.head(1)
first.loc[:, ("A", "B", "C", "D")] = a
print(first)
big_table = pd.concat([big_table, first])
print('-0-' * 50)
print(big_table)