要按组从多个列中获取最大值,可以使用groupby函数和max函数来实现。下面是一个示例代码:
import pandas as pd
# 创建一个包含多个列的DataFrame
data = {'Group': ['A', 'A', 'B', 'B', 'C', 'C'],
'Col1': [1, 2, 3, 4, 5, 6],
'Col2': [2, 4, 6, 8, 10, 12],
'Col3': [3, 6, 9, 12, 15, 18]}
df = pd.DataFrame(data)
# 按组从多个列中获取最大值
max_values = df.groupby('Group').max()
print(max_values)
输出结果为:
Col1 Col2 Col3
Group
A 2 4 6
B 4 8 12
C 6 12 18
在这个示例中,我们创建了一个包含Group、Col1、Col2和Col3列的DataFrame。然后,我们使用groupby函数将数据按Group列进行分组,并使用max函数获取每个组中的最大值。最后,我们打印出结果。
上一篇:按组创建组合
下一篇:按组从多列中随机抽取经验分布的值