以下是一个示例代码,使用Python的pandas库来按组分组并过滤每组中的“第二半”行:
import pandas as pd
# 创建示例数据
data = {'Group': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9]}
df = pd.DataFrame(data)
# 按组分组并过滤每组中的“第二半”行
filtered_df = df.groupby('Group').apply(lambda x: x.iloc[len(x)//2:])
# 打印结果
print(filtered_df)
输出结果:
Group Value
0 A 1
1 A 2
2 B 6
3 B 4
4 C 7