所以这是我第一次使用 pandas,除了不相关的问题之外我能找到的唯一信息是在文档中,我读了好几次,我不确定我做错了什么,我的尝试是从 CSV 中提取用户信息文件和我用来计算它的当前程序只是打印收集的数据,但每次我运行该程序时,它都会弹出一个 KeyError 但我的列名称是基于表中的名称和用户 ID 的数字,但无论它是字符串还是整数说同样的 KeyError(1)。
(敏感信息已删除或替换)
输入: (ID:1 | RID:2)
import pandas as pd
import numpy as np
UDS = pd.read_csv("AEAUserData.csv")
df = pd.Series(["0", "1", "2", "3"], index=["0", "1", "2", "3"])
ID = input("Enter User ID: ")
RID = input("Enter receiving ID: ")
UNN = ""
PWN = ""
acitveUN = UDS.at[int(ID), 1]
activePW = UDS.at[ID, 4]
activeUS = UDS.at[ID, 2]
activeENC = UDS.at[ID, 5]
receivingUN = UDS.at[RID, 1]
receivingPW = UDS.at[RID, 4]
receivingUS = UDS.at[RID, 2]
receivingENC = UDS.at[RID, 5]
/*
commence printing everything
*\
AEA用户数据.csv
0,1, 2, 3, 4, 5, 6,
1,FamFighter123,Lt. Dan,5,Password123,Soaring Eagle,
2,BubbaShrimp123,Gen. Bubba,3,Password456,Shrimp Boat,
3,1Forest1,Pfc. Gump,2,Password789,Life is like a box of chocolates,
输出:
Enter User ID: 1
Enter receiving ID: 2
Traceback (most recent call last):
File "C:\Users\**********\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
return self._engine.get_loc(casted_key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc
File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\***********\Desktop\********\******\Debug_Tester.py", line 9, in <module>
acitveUN = UDS.at[int(ID), 1]
~~~~~~^^^^^^^^^^^^
File "C:\Users\******\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexing.py", line 2575, in __getitem__
return super().__getitem__(key)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\**********\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexing.py", line 2527, in __getitem__
return self.obj._get_value(*key, takeable=self._takeable)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\***********\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\frame.py", line 4214, in _get_value
series = self._get_item_cache(col)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\************\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\frame.py", line 4638, in _get_item_cache
loc = self.columns.get_loc(item)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\*********\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc
raise KeyError(key) from err
KeyError: 1
我尝试更改会带来其他错误的范围,并阅读文档,也许我是文盲,但除了我没有正确创建数据框之外,我找不到发生这种情况的原因。
结果我不太明白 CSV 文件是如何读取的,经过一些调整后它可以解决 0 个问题。
import numpy as np
UDS = pd.read_csv("AEAUserData.csv")
df = pd.Series(["0", "1", "2", "3"], index=["0", "1", "2", "3"])
ID = int(input("Enter User ID: "))
RID = int(input("Enter receiving ID: "))
UNN = ""
PWN = ""
acitveUN = UDS.at[ID, "UN"]
activePW = UDS.at[ID, "Password"]
activeUS = UDS.at[ID, "User"]
activeENC = UDS.at[ID, "ENC"]
receivingUN = UDS.at[RID, "UN"]
receivingPW = UDS.at[RID, "Password"]
receivingUS = UDS.at[RID, "User"]
receivingENC = UDS.at[RID, "ENC"]
print(UDS)
print(ID)
print(RID)
print(UNN)
print(PWN)
print(acitveUN)
print(activeENC)
print(activePW)
print(activeUS)
print(receivingUN)
print(receivingPW)
print(receivingENC)
print(receivingUS)```
`ID,UN,User,ENC,Password,Phrase
01,FamFighter123,Lt. Dan,5,Password123,Soaring Eagle
02,BubbaShrimp123,Gen. Bubba,3,Password456,Shrimp Boat
03,1Forest1,Pfc. Gump,2,Password789,Life is like a box of chocolates`