【发布时间】:2021-10-15 07:20:21
【问题描述】:
美好的一天。我正在使用 pandas 读取 xlsx 文件中的一列数据。我只有 1 列,里面塞满了不同年龄的人。这是我的代码:
import math
import pandas as pd
import openpyxl
df = pd.read_excel('/run/media/soumi/Luna (HDD)/CF-0001/PSHS/Statistics/Module 1/SLG 3.0 Data set 3.1 DOH COVID Data Drop Case Information.xlsx') # can also index sheet by name or fetch all sheets
num = df['B2:B57001;'].tolist()
num.sort()
print(f'\n{num}')
fdt = pd.Series(num).value_counts().sort_index().reset_index().reset_index(drop=True)
fdt.columns = ['Element', 'Frequency']
print (fdt)
我希望它会为我创建一个频率表,但是,当我运行它时,它会向我显示:
python -u "/run/media/soumi/Luna (HDD)/CF-0001/Programming/Python/Stat.py"
Traceback (most recent call last):
File "/home/soumi/.local/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3361, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'B2:B57001;'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/run/media/soumi/Luna (HDD)/CF-0001/Programming/Python/Stat.py", line 6, in <module>
num = df['B2:B57001;'].tolist()
File "/home/soumi/.local/lib/python3.9/site-packages/pandas/core/frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "/home/soumi/.local/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 'B2:B57001;'
我应该怎么做才能解决这个问题?提前致谢
编辑 1:我还要补充一点,我只使用 Manjaro,我不使用 Windows,所以我没有 excel 应用程序...如果有帮助
【问题讨论】:
-
df['B2:B57001;']基本上会寻找名为B2:B57001;的列
标签: python xlsx openoffice.org openoffice-calc