对活动团队进行排序

时间:2019-04-23 13:28:16

标签: python pandas

我正在尝试为机器人比赛排名。他们将分三个阶段进行比赛。他们在每个阶段都有总分和持续时间。示例数据如下:

sample image of data

如果我不必比较时间,则可以使用python的“ max()”函数。

data["max"] = data[["stage1","stage2","stage3"]].max(axis=1)

我使用熊猫库的“ sort_values”方法对它们进行排序。为了对球队进行排名,我必须在3个阶段中找到最佳成绩。如果我不看时间,可以通过在max方法中插入分数来找到最佳分数,但是如果分数相等,则应将其与最佳时间进行比较。为了能够取得最佳成绩和时间以获得足够的成绩。

对于创建数据:

import pandas as pd
import numpy as np
import random

data = pd.DataFrame()

puanlar=[]
for sayi in np.ones(100):
    puanlar.append(sayi*random.randrange(1,100))
puanlar2 = []
for sayi in np.ones(100):
    puanlar2.append(sayi*random.randrange(1,100))
puanlar3 = []
for sayi in np.ones(100):
    puanlar3.append(sayi*random.randrange(1,100))
data["stage1"] = puanlar
data["stage2"] = puanlar2
data["stage3"] = puanlar3

dakika=[]
saniye = []
salise = []
for sayi in np.ones(100):
    dakika.append(random.randrange(1,4))
    saniye.append(random.randrange(1,60))
    salise.append(random.randrange(1,60))

data["stage1_minute"] = dakika
data["stage1_second"] = saniye
data["stage1_millisecond"] = salise
dakika=[]
saniye = []
salise = []
for sayi in np.ones(100):
    dakika.append(random.randrange(1,4))
    saniye.append(random.randrange(1,60))
    salise.append(random.randrange(1,60))

data["stage2_minute"] = dakika
data["stage2_second"] = saniye
data["stage2_millisecond"] = salise
dakika=[]
saniye = []
salise = []
for sayi in np.ones(100):
    dakika.append(random.randrange(1,4))
    saniye.append(random.randrange(1,60))
    salise.append(random.randrange(1,60))

data["stage3_minute"] = dakika
data["stage3_second"] = saniye
data["stage3_millisecond"] = salise
data["max"] = data[["stage1","stage2","stage3"]].max(axis=1)
data.sort_values(['max'], ascending=False,inplace=True)

1 个答案:

答案 0 :(得分:0)

我将创建对应列的某种组合并找到max:

np.random.seed(2019)
data_len = 1000
data = pd.DataFrame({'stage1': np.random.randint(1,10, data_len),
                     'stage2': np.random.randint(1,10, data_len),
                     'stage3': np.random.randint(1,10, data_len),
                     'stage1_min': np.random.uniform(1,4, data_len),
                     'stage1_sec': np.random.uniform(1,60, data_len),
                     'stage1_mil': np.random.uniform(1,60, data_len),
                     'stage2_min': np.random.uniform(1,4, data_len),
                     'stage2_sec': np.random.uniform(1,60, data_len),
                     'stage2_mil': np.random.uniform(1,60, data_len),
                     'stage3_min': np.random.uniform(1,4, data_len),
                     'stage3_sec': np.random.uniform(1,60, data_len),
                     'stage3_mil': np.random.uniform(1,60, data_len)})

# combination
for i in range(1,4):
    prefix = f'stage{i}'
    data[prefix+'new'] = [(stage, -m,-s, -ms) for stage, m, s, ms in zip(data[prefix],
                                                                       data[prefix+'_min'],
                                                                       data[prefix+'_sec'],
                                                                       data[prefix+'_mil'])]

data['max'] = data[[f'stage{i}new' for i in range(1,4)]].max(axis=1)

您也可以将idxmax()用于具有最大值的行的索引。