python - KeyError 0 在 pandas 中选择带有字符串的位置

标签 python string python-3.x pandas

一位 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

关于python - KeyError 0 在 pandas 中选择带有字符串的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46288216/

相关文章:

python-3.x - 我在用这些引号做错什么了?

python - 从姓名列表中生成学生对,每周新对

python - pandas.DataFrame.round 似乎不适用于我的 DataFrame - 舍入问题导致额外的数据存储在 csv 文件中

python - 如何在我的 App Engine 应用程序中请求本地 URL?

c++ - 如何初始化对象数组?

python - 如何在 matplotlib "histogram"图表上添加(或注释)值标签(或频率)

C字符串函数错误

php - 如果不存在,如何将 .jpg 添加到字符串末尾

python - 如何解析具有相同键的多个字典值

python - 在Python中,带小数部分的数字的十进制到十六进制转换?