重构 Gamble 数据库进行比较

问题描述 投票:0回答:1

对于一个业余项目,我正在比较 2 个博彩机构的赔率,但在将这些信息以正确的格式放入我的表格中时遇到了一些麻烦。

目前我有下表:

提供商 投注类型 结果标签 赔率
A机构 A 高于1.5 1.3
A机构 A 低于1.5 1.6
A机构 B 是的 2.3
A机构 B 没有 1.2
B机构 A 高于1.5 1.1
B机构 A 低于1.5 1.8
B机构 B 是的 1.5
B机构 B 没有 1.3

我想要的是以行如下所示的方式转换此表:

提供商 投注类型 结果标签 赔率 对面提供商 相反的结果标签 相反的赔率
A机构 A 高于1.5 1.3 B机构 低于1.5 1.8
A机构 A 低于1.5 1.6 B机构 高于1.5 1.1
A机构 B 是的 2.3 B机构 没有 1.3
A机构 B 没有 1.2 B机构 是的 1.5

此转换会剪切一半的行并将其粘贴为列。值得注意的是,这里的 OppositeOutcomeLabel 与 OutcomeLabel 是相反的(命名暴露了它,但我想我会突出显示它)。

我做了一些准备工作,但似乎无法弄清楚最后一步。任何帮助都会很棒!

下面的示例只是数据的一个子集。我正在寻找一种可扩展到未来的动态解决方案(除了指定对立面之外)

提前致谢

data = {
    'Provider': ['Agency A', 'Agency A', 'Agency A', 'Agency A', 'Agency B', 'Agency B', 'Agency B', 'Agency B'],
    'MainBet': ['A', 'A', 'B', 'B', 'A', 'A', 'B', 'B'],
    'OutcomeLabel': ['Higher than 1.5', 'Lower than 1.5', 'Yes', 'No', 'Higher than 1.5', 'Lower than 1.5', 'Yes', 'No'],
    'Odds': [2.0, 3.0, 1.5, 2.0, 1.0, 1.0, 1.0, 1.0]
}

df = pd.DataFrame(data)

# Create a dictionary to map OutcomeLabel to its opposite
opposite_outcome_label = {
    'Higher than 1.5': 'Lower than 1.5',
    'Lower than 1.5': 'Higher than 1.5',
    'Yes': 'No',
    'NO': 'Yes'
}

# Create a dictionary to map Provider to its opposite
opposite_provider = {
    'Agency A': 'Agency B',
    'Agency B': 'Agency A'
}

# Function to get opposite OutcomeLabel
def get_opposite_outcome(row):
    return opposite_outcome_label.get(row['OutcomeLabel'])


# Function to get opposite Provider
def get_opposite_provider(row):
    return opposite_provider.get(row['Provider'])

# Apply the functions to create OppositeOutcome and OppositeProvider columns
df['OppositeOutcome'] = df.apply(get_opposite_outcome, axis=1)
df['OppositeProvider'] = df.apply(get_opposite_provider, axis=1)
python pandas dataframe data-manipulation
1个回答
0
投票

尝试:

opposite_outcome_label = {
    "Higher than 1.5": "Lower than 1.5",
    "Lower than 1.5": "Higher than 1.5",
    "Yes": "No",
    "No": "Yes",
}

(i1, g1), (i2, g2) = df.groupby("Provider")

# swap in case Agency B is first
if i1 == "Agency B":
    i1, g1, i2, g2 = i2, g2, i1, g1

# sort g1 according keys of `opposite_outcome_label`
g1 = g1.sort_values(
    by="OutcomeLabel",
    key=lambda series, l=list(opposite_outcome_label): [l.index(v) for v in series],
)

# sort g2 according values of `opposite_outcome_label`
g2 = g2.sort_values(
    by="OutcomeLabel",
    key=lambda series, l=list(opposite_outcome_label.values()): [
        l.index(v) for v in series
    ],
)

out = pd.concat(
    [g1.reset_index(drop=True), g2.add_prefix("Opposite").reset_index(drop=True)],
    axis=1,
)
print(out)

打印:

   Provider BettingType     OutcomeLabel  Odds OppositeProvider OppositeBettingType OppositeOutcomeLabel  OppositeOdds
0  Agency A           A  Higher than 1.5   1.3         Agency B                   A       Lower than 1.5           1.8
1  Agency A           A   Lower than 1.5   1.6         Agency B                   A      Higher than 1.5           1.1
2  Agency A           B              Yes   2.3         Agency B                   B                   No           1.3
3  Agency A           B               No   1.2         Agency B                   B                  Yes           1.5
© www.soinside.com 2019 - 2024. All rights reserved.