一位朋友发给我这个虚拟代码,为了做一个简单的密码:
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]
。但有人知道为什么第一条线有效吗?我认为迭代字符串仍然会返回一个字符串。也许我错过了一些东西,它来自大熊猫。我正在运行版本'0.20.3'
编辑:我发布时忘了[0]
在a
的定义中它应该是:
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
如果您的退货未标记为“0”,那么您将返回一个关键错误。例如,你的回报是这样的:
4 a
Name: Value, dtype: object
然后,是的,你得到了一个keyerror。在Pandas中,你想为整数位置做iloc以找到第一条记录。使用.iloc
:
word1="helloworld"
for char in word1:
h=code.loc[code['Key'] == char, 'Value'].iloc[0]
word1 = 'helloworld'
encoding=[]
for char in word1:
h = code.loc[code['Key'] == char, 'Value'].iloc[0]
encoding.append(h)
enc = ''.join(encoding)
print(enc)
输出:
uayyzvzeyf
正如@FabianP在评论中指出:
这里不需要iloc。与loc一起使用的切片已经返回一个字符(作为一个系列),所以这里[0]尝试索引该字符,该字符不可迭代
它不返回一个数组,因此当你使用'[0]'进行切片时,你不需要第一个元素,但它更像是一个基于索引的切片 - 因为该对象是一个pd.Series
。它对char'a'起作用的事实是由于'a'是liste1
的第一个字母,因此其索引为0。