Skip to main content
 首页 » 编程设计

python之KeyError 0 loc 选择与 Pandas 中的字符串

2025年02月15日18wuhuacong

一个 friend 发给我这个伪代码,为了做一个简单的密码:

chaine1="abcdefghijklmnopqrstuvwxyz" 
chaine2="abcdefghijklmnopqrstuvwxyz" 
list1=list(chaine1) 
list2=list(chaine2) 
rnd.shuffle(list2) 
code=pd.DataFrame({"Key": list1, "Value" : list2}) 

他设法恢复了这样一封信:

a = code.loc[code['Key'] == 'a', 'Value'] 

所以他试图遍历这个词来对其进行编码:

word1="helloworld" 
for char in word1: 
    h=code.loc[code['Key'] == char, 'Value'][0] 

语法看起来一样,但失败了:

KeyError                                  Traceback (most recent call last) 
<ipython-input-88-4e2a59e0978e> in <module>() 
      1 word1="helloworld" 
      2 for char in word1: 
----> 3     h=code.loc[code['Key'] == char, 'Value'][0] 
 
~/Envs/test_bapt/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key) 
    599         key = com._apply_if_callable(key, self) 
    600         try: 
--> 601             result = self.index.get_value(self, key) 
    602  
    603             if not is_scalar(result): 
 
~/Envs/test_bapt/lib/python3.5/site-packages/pandas/core/indexes/base.py in get_value(self, series, key) 
   2475         try: 
   2476             return self._engine.get_value(s, k, 
-> 2477                                           tz=getattr(series.dtype, 'tz', None)) 
   2478         except KeyError as e1: 
   2479             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']: 
 
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value (pandas/_libs/index.c:4404)() 
 
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value (pandas/_libs/index.c:4087)() 
 
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5126)() 
 
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item (pandas/_libs/hashtable.c:14031)() 
 
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item (pandas/_libs/hashtable.c:13975)() 
 
KeyError: 0 

我发现 .values 丢失了:h=code.loc[code['Key'] == char, 'Value'][0]。 但是有人知道为什么第一行有效吗?我认为迭代一个字符串仍然会返回一个字符串。也许我遗漏了一些东西,它来自 Pandas 。我正在运行版本 '0.20.3'

编辑: 当我发帖时,我忘记了 a 定义中的 [0] 它应该是:

a = code.loc[code['Key'] == 'a', 'Value'][0] 

很抱歉,我完全避开了我帖子的重点。我想了解为什么它在这个简单的案例中有效,而不是在迭代期间。

请您参考如下方法:

只需去掉h属性末尾的[0]即可:

import random as rnd 
import pandas as pd 
 
chaine1="abcdefghijklmnopqrstuvwxyz" 
chaine2="abcdefghijklmnopqrstuvwxyz" 
list1=list(chaine1) 
list2=list(chaine2) 
rnd.shuffle(list2) 
code=pd.DataFrame({"Key": list1, "Value" : list2}) 
 
word1="helloworld" 
for char in word1: 
    print(char) 
    h=code.loc[code['Key'] == char, 'Value'] 
    print(h) 

我添加了两个打印以确保代码确实按照他应该的方式执行,我得到以下结果:

h 
7    e 
Name: Value, dtype: object 
e 
4    m 
Name: Value, dtype: object 
l 
11    t 
Name: Value, dtype: object 
l 
11    t 
Name: Value, dtype: object 
o 
14    j 
Name: Value, dtype: object 
w 
22    d 
Name: Value, dtype: object 
o 
14    j 
Name: Value, dtype: object 
r 
17    i 
Name: Value, dtype: object 
l 
11    t 
Name: Value, dtype: object 
d 
3    f 
Name: Value, dtype: object