使用pandas.DataFrame.idxmax并希望使用联合参数

2024-04-29 19:29:00 发布

您现在位置:Python中文网/ 问答频道 /正文

图像中的每一行代表一场棒球比赛。试图让idxmax方法返回一个不同的单词或数字以显示它们在列中的最大值

Each row in the image represents a baseball game. Trying to getting the idxmax method to return a different word or number to show when they're is a joint maximum in the columns

这是我用来获得跑数最多的那一局的代码。它返回具有第一个最大值的索引,我试图找到一种方法来显示输出中显示联合最大值的内容(即两局或更多局的得分最高):

df['maxInningsRuns'] = sbr[['1ITotRuns','2ITotRuns','3ITotRuns','4ITotRuns','5ITotRuns','6ITotRuns','7ITotRuns','8ITotRuns','9ITotRuns','XITotRuns']].idxmax(axis=1)```

Tags: 方法代码图像内容df代表数字单词
1条回答
网友
1楼 · 发布于 2024-04-29 19:29:00

不要张贴代码的图片。实际上,由于您是新手,所以提供了人们可以使用的代码,这相对来说比较容易做到,我在下面做了这一点,因此在将来创建/提供一些数据集示例时遵循这一点)

获取该值并使用该值对数据帧进行切片。或者只是遍历每个列列表,看看值是否在其中。有多种方法可以做到这一点

import pandas as pd

rows = [[3,0,1,0,1,0,0,2,1,0],
        [0,0,1,1,0,0,5,2,0,0],
        [0,0,0,0,0,0,1,0,0,0],
        [2,0,1,1,1,1,2,0,2,0],
        [0,0,0,4,5,0,0,1,0,0],
        [0,0,1,0,1,4,1,0,0,0]]



cols = ['1ITotRuns','2ITotRuns',
        '3ITotRuns','4ITotRuns',
        '5ITotRuns','6ITotRuns',
        '7ITotRuns','8ITotRuns',
        '9ITotRuns','XITotRuns']

sbr = pd.DataFrame(rows, columns = cols)

# Returns the max values of each inning
max_each_column = sbr[cols].max()

# Gets 1 column where that max value is found
idx_max_value = sbr[cols].max().idxmax()

# Use the column the get that value (5)
max_value = sbr[idx_max_value].max()

#Find columns that have 5 in it
max_innings = [col for col in cols if max_value in list(sbr[col])]
print(max_innings)

输出:

['5ITotRuns', '7ITotRuns']

相关问题 更多 >