使用 Excel Pandas 中的浮点值填充字典的问题
Posted
技术标签:
【中文标题】使用 Excel Pandas 中的浮点值填充字典的问题【英文标题】:Issues populating dictionary with float values from Excel Pandas 【发布时间】:2018-12-09 13:08:09 【问题描述】:我正在使用 Excel 电子表格来填充字典。然后我使用这些值将另一个数据帧的值乘以引用,但是当我尝试时它给了我错误。我决定将excel电子表格从我的字典中取出以避免错误,但我没有成功。我这样做是因为字典最终会变得很长,并且编辑键及其值太乏味了。我正在使用 Python 2.7
import pandas as pd
#READ EXCEL FILE
df = pd.read_excel("C:/Users/Pedro/Desktop/dataframe.xls")
#Store the keys with its value in a dictionary. This will become df2
d = "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 spreadsheet
df5 = pd.DataFrame.from_dict(d, orient='index')
df5.to_excel('bob_dict.xlsx')
#populatethe dictionary from the excel spreadsheet
df2 = pd.read_excel("C:/Users/Pedro/Desktop/bob_dict.xlsx")
#Convert dtframe back to a dictionary
dictionary = df2.to_dict(orient='dict')
#Pass the dictionary as reference
b = df.filter(like ='Value').values
c = df.filter(like ='ZONE').replace(dictionary).astype(float).values
df['pro_cum'] = ((c * b).sum(axis =1))
运行时我得到 ValueError: could not convert R6B string to float.
c = df.filter(like ='ZONE').replace(d).astype(float).values
但如果我用原始字典替换区域值,它运行时不会出错。
输入:df
HP ZONE Value ZONE1 Value1
3 R7A 0.7009 M1-4/R6B 0.00128
2 R6A 0.5842 M1-4/R7A 0.00009
7 M1-6/R10 0.1909 M1-4/R6A 0.73576
9 R6B 0.6919 PARK 0.03459
6 PARK 1.0400 M1-4/R6A 0.33002
9.3 M1-4/R6A 0.7878 PARK 0.59700
10.6 M1-4/R6B 0.0291 R6A 0.29621
11.9 R9 0.0084 M1-4 0.00058
13.2 M1-5/R10 0.0049 M1-4 0.65568
14.5 M1-4/R7A 0.0050 C5-3 0.00096
15.8 M1-5/R7-3 0.0189 C5-3 1.59327
17.1 M1-5/R9 0.3296 M1-4/R6B 0.43918
18.4 C5-3 0.5126 R6B 0.20835
19.7 M1-4 0.5126 PARK 0.22404
【问题讨论】:
【参考方案1】:字典d
之外的一些值存在问题(错误说R6B
,但可能存在更多值),因此无法转换为浮点数。
你可以找到这个值:
#create Series from all Zone columns
vals = df.filter(like ='ZONE').replace(d).stack()
#for non numeric return NaNs, so filtering return problematic values
out = vals[pd.to_numeric(vals, errors= 'coerce').isnull()].unique()
print (out)
然后添加到字典d
以避免此错误。
示例:
print (df)
HP ZONE Value ZONE1 Value1
0 3.0 R7A 0.7009 M1-4/R6B 0.00128
1 2.0 R6A 0.5842 M1-4/R7A 0.00009
2 7.0 M1-6/R10 0.1909 M1-4/R6A 0.73576
3 9.0 R6B 0.6919 PARK 0.03459
4 6.0 PARK 1.0400 M1-4/R6A 0.33002
5 9.3 M1-4/R6A 0.7878 PARK 0.59700
6 10.6 M1-4/R6B 0.0291 R6A 0.29621
7 11.9 R9 0.0084 M1-4 0.00058
8 13.2 M1-5/R10 0.0049 M1-4 0.65568
9 14.5 M1-4/R7A 0.0050 C5-3 0.00096
10 15.8 M1-5/R7-3 0.0189 C5-3 1.59327
11 17.1 M1-5/R9 0.3296 M1-4/R6B 0.43918
12 18.4 C5-3 0.5126 R6B 0.20835
13 19.7 M1-4 0.5126 PARK1 0.22404 <- added PARK1 for testing
d = "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
vals = df.filter(like ='ZONE').replace(d).stack()
out = vals[pd.to_numeric(vals, errors= 'coerce').isnull()].unique()
print (out)
['PARK1']
【讨论】:
好的,我看到填充字典中的大部分值都无缘无故地变成了 unicode 字符串。[u'M1-4' u'C5-3' u'M1-4/R7A' u'R7A' u'M1-4/R6B' u'R6B' u'M1-5/R7-3' u'M1-5/R9' u'M1-6/R10' u'R6A' u'M1-4/R6A' u'PARK' u'M1-5/R10']
有没有将 unicode 字符串转换为浮点数的方法。我看到 float
无法正常工作,因为我使用的是 Python 2.7.10
hmmm,一个想法,如果在每个字符串之前手动添加 u
就像前两个值一样 - d = u"M1-4":0.60,u"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
它工作吗?
感谢Jezrael的帮助,不过我解决了这个问题你可以看看我的回答。【参考方案2】:
我能够解决我的问题。当我将字典转换为数据框时,键成为索引,因此当我将数据框转换回字典时,我最终得到了一个字典字典。所以我不得不在 replace 方法中说明这一点。
0: 'M1-4': 0.6, '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.2, 'M1-6/R10': 0.85, 'R6B': 0.85,
'R9': 0.85, 'M1-5/R9': 0.85
所以我编辑了这行代码并添加了[0]
c = df.filter(like='ZONE').replace(dictionary[0]).astype(float).values
【讨论】:
以上是关于使用 Excel Pandas 中的浮点值填充字典的问题的主要内容,如果未能解决你的问题,请参考以下文章