为了账号安全,请及时绑定邮箱和手机立即绑定

使用Excel Pandas中的浮点值填充字典时出现问题

使用Excel Pandas中的浮点值填充字典时出现问题

汪汪一只猫 2021-03-29 12:13:39
我正在使用Excel电子表格来填充字典。然后,我使用这些值将另一个数据帧的值乘以引用,但是尝试时会给我错误。为了避免错误,我决定将excel电子表格制作成字典,但是我没有成功。我这样做是因为字典最终会变长,并且编辑键及其值太繁琐了。我正在使用Python 2.7import pandas as pd#READ EXCEL FILEdf = pd.read_excel("C:/Users/Pedro/Desktop/dataframe.xls")#Store the keys with its value in a dictionary. This will become df2d = {"M1-4":0.60,"M1-5/R10":0.85,"C5-3":0.85,"M1-5/R7-3":0.85,"M1-4/R7A":0.85,"R7A":0.85,"M1-4/R6A":0.85,"M1-4/R6B":0.85,"R6A":0.85,"PARK":0.20,"M1-6/R10":0.85,"R6B":0.85,"R9":0.85,"M1-5/R9":0.85}#Convert the dictionary to an Excel spreadsheetdf5 = pd.DataFrame.from_dict(d, orient='index')df5.to_excel('bob_dict.xlsx')#populatethe dictionary from the excel spreadsheetdf2 = pd.read_excel("C:/Users/Pedro/Desktop/bob_dict.xlsx")#Convert dtframe back to a dictionarydictionary = df2.to_dict(orient='dict')#Pass the dictionary as reference b = df.filter(like ='Value').valuesc = df.filter(like ='ZONE').replace(dictionary).astype(float).valuesdf['pro_cum'] = ((c * b).sum(axis =1))运行时,我得到ValueError:无法将R6B字符串转换为float。c = df.filter(like ='ZONE').replace(d).astype(float).values但是,如果我用原始字典替换区域值,则它会正常运行。输入:dfHP    ZONE           Value  ZONE1       Value13     R7A           0.7009  M1-4/R6B    0.001282     R6A           0.5842  M1-4/R7A    0.000097     M1-6/R10      0.1909  M1-4/R6A    0.735769     R6B           0.6919  PARK        0.034596     PARK          1.0400  M1-4/R6A    0.330029.3   M1-4/R6A      0.7878  PARK        0.5970010.6  M1-4/R6B      0.0291  R6A         0.2962111.9  R9            0.0084  M1-4        0.0005813.2  M1-5/R10      0.0049  M1-4        0.6556814.5  M1-4/R7A      0.0050  C5-3        0.0009615.8  M1-5/R7-3     0.0189  C5-3        1.5932717.1  M1-5/R9       0.3296  M1-4/R6B    0.4391818.4  C5-3          0.5126  R6B         0.2083519.7  M1-4          0.5126  PARK        0.22404
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 202 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信