我想在数据框上使用数据透视表 - 它在一台计算机上工作正常,但在第二台计算机上出现错误
我不明白这个问题
df = pd.DataFrame({
"A": ['a', 'b', 'c', 'd', 'e', 'f'],
"B": [1, 1, 2, 1, 1, 2],
"C": [1, 2, 1, 2, 1, 2],
"V": [0, 1, 2, 3, 4, 5]})
D1 = df.pivot(index="A", columns=["B", "C"], values="V")
错误:
ValueError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py in _get_level_number(self, level)
1294 try:
-> 1295 level = self.names.index(level)
1296 except ValueError:
ValueError: 'B' is not in list
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-77-789369e35926> in <module>
----> 1 D1=df.pivot(index="A", columns=["B", "C"],values="V")
2 D1
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in pivot(self, index, columns, values)
5921 from pandas.core.reshape.pivot import pivot
5922
-> 5923 return pivot(self, index=index, columns=columns, values=values)
5924
5925 _shared_docs[
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\pivot.py in pivot(data, index, columns, values)
448 else:
449 indexed = data._constructor_sliced(data[values].values, index=index)
--> 450 return indexed.unstack(columns)
451
452
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in unstack(self, level, fill_value)
3548 from pandas.core.reshape.reshape import unstack
3549
-> 3550 return unstack(self, level, fill_value)
3551
3552 # ----------------------------------------------------------------------
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in unstack(obj, level, fill_value)
396 # _unstack_multiple only handles MultiIndexes,
397 # and isn't needed for a single level
--> 398 return _unstack_multiple(obj, level, fill_value=fill_value)
399 else:
400 level = level[0]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in _unstack_multiple(data, clocs, fill_value)
318 index = data.index
319
--> 320 clocs = [index._get_level_number(i) for i in clocs]
321
322 rlocs = [i for i in range(index.nlevels) if i not in clocs]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in <listcomp>(.0)
318 index = data.index
319
--> 320 clocs = [index._get_level_number(i) for i in clocs]
321
322 rlocs = [i for i in range(index.nlevels) if i not in clocs]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py in _get_level_number(self, level)
1296 except ValueError:
1297 if not is_integer(level):
-> 1298 raise KeyError(f"Level {level} not found")
1299 elif level < 0:
1300 level += self.nlevels
KeyError: 'Level B not found'
我正在使用 Pandas 的版本 2.2.1 并且您展示的代码可以工作。我也尝试过 Pandas 1.5.3。
一些建议:
pip show pandas
。requirements.txt
文件。如果您想了解更多信息,请阅读:Python Requirements.txt。简而言之:您使用的所有库(Pandas、NumPy 等)通常都使用名为 Pip 的 Python 包管理器进行处理。这些软件包有版本,有时在给定版本中有效的东西可能在不同版本中失败。使用 requirements.txt
是一种表达方式:“该项目适用于这些版本中的这些包”。如果您在计算机中共享了导致代码失败的 Pandas 版本,也许我们可以为您提供有关该特定问题的支持。但我真的建议您只需使用
requirements.txt
文件来确保您的代码在(理想情况下)任何计算机中都能按照您的预期工作。