EDIT_01:我刚刚注意到我的方法不能正常工作,因为它从保存的excel文件中删除了"Horse“字段。EDIT_02:我有工作要做,但我相信一定有更好的方法.删除df1中的“赔率”列,然后将df1与df_odd合并,然后保存df1。这仍然将"Horse“列移动到第一列(我猜想,由于索引声明),但我可以接受这一点。
谢谢您的建议。我有一个Excel文件的马(赔率)的细节,我想更新。例如:
Draw Form Horse Age Weight OR Jockey Trainer Allow RTF TS RPR Days Aid WtLbs CDB Odds
6 05035- Buy Me Back 5 9-7 74 Luke Morris Mark Loughnane 52 74 85 37 133 C D SP
2 /2473- Alablaq 4 9-6 73 George Downing David Evans 41 72 84 17 132 SP
4 1018-3 Helvetian 7 9-5 72 Sean Levey Kevin Frost 29 78 85 4 t 131 D
5 32676- Vape 5 9-1 68 Hollie Doyle Tony Carroll 26 71 87 28 127 D SP我想用来自df_odd的相应马的“赔率”数据来更新“赔率”列。df_odd示例:
Horse Course Time Date Odds
0 Abnaa Newcastle 15:10 2022-01-06
1 Affoburg Chepstow 15:20 2022-01-06 66/1目前,我可以创建一个"RC_Odds.xlsx“文件并将其读入python,然后使用df1"Odds”= df1"Odds“。
我假设有一种更简洁的方法来完成同样的操作,而不需要生成"RC_Odds.xlsx“文件,但我无法解决语法问题。任何帮助都是欢迎的。谢谢。
我的工作代码是:
df_odd = pd.DataFrame(get_odds(date_ext))
df_odd.to_excel('Racecards\RC_Odds.xlsx', index = False)
# reading two csv files
df1 = pd.read_excel('Racecards/Racecards.xlsx', index_col="Horse")
df2 = pd.read_excel('Racecards/RC_Odds.xlsx', index_col="Horse")
del df1["Odds"]
df1 = pd.merge(df1, df2, on ='Horse', how ="left")
df1 = df1.reset_index()
df1.to_excel(f'{"Racecards/Racecards"}.xlsx', index = False)
print("Odds Updated")发布于 2022-01-08 14:19:07
它删除Horse列的原因似乎是因为您的代码中的最后一行:
df1.to_excel(f'{"Racecards/Racecards"}.xlsx', index = False)您要么在导入时不使用index_col = "Horse",要么在导出时不使用index = False。如果需要更新文件上的"Horse",则不能同时执行这两项操作。
index = False将从导出中删除索引,因此,通过将索引设置为Horse,然后声明您不希望它作为输出的一部分,它将被删除。
为了更改值,可以通过创建Python读取的文件来继续更新,然后使用更新文件(RC赔率)来更新原始文件(Racecards)。另一种方法是在Excel中直接更新,但我认为您不想这样做。另一种方法是使用input语句并使用这些输入构建更新。
例如,您可以先声明更新的数量,然后在此基础上循环遍历从0到num_updates-1的每个数字,同时获取将添加到字典中的名称和概率的输入。
horse_updates = {}
num_updates = input("Number of Updates to make: ")
for x in range(num_updates-1):
horse_name = input("Horse Name: ")
horse_odd = input("Horse Odds: ")
horse_updates["name"] = horse_odd然后,您应该能够获得该字典,并循环更新。如果字典令人困惑,您也可以选择列出列表。
发布于 2022-01-12 16:54:57
感谢您的输入--这似乎是最简单的解决方案(删除df1中的“赔率”列,然后将df1与df_odd合并,然后保存df1)。
所以我会关闭这条线。再次感谢
https://stackoverflow.com/questions/70625534
复制相似问题