Python(pandas)-在Excel文件中选择特定列

eyh26e7m  于 2023-05-12  发布在  Python
关注(0)|答案(1)|浏览(124)

我是**Python(3.11.3)**w/pandas的初学者,我已经处理这个问题几个小时了
我怀疑这个问题是由selected_A = random.sample(list(df['A']), 80)引起的,看起来我的代码在Excel文件中找不到“A”列,但我不知道如何修复它。:/
欢迎提出建议!

目标:

从“OriginalPairedList.xlsx”中的“A”列中随机选择80个单元格,并将所选单元格保存在D列中。

本期:

当前代码无法从我的excel文件(OriginalPairedList.xlsx)中选择/读取我想要的列(A),因此无法执行。

我的编码:

import pandas as pd
import os
import random
os.chdir("TLBX_experiment/pilotZone/expicture")

# read excel file
df = pd.read_excel("OriginalPairedList.xlsx")

# select 80 cells from column A and store in column D
selected_A = random.sample(list(df['A']), 80)
df['D'] = selected_A

终端消息:

jamesabcd@PodeMacBook-Pro Desktop % /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 /Users/jamesabcd/Desktop/TLBX_exp
eriment/pilotZone/expicture/selectAB4.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 3652, in get_loc
    return self._engine.get_loc(casted_key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pandas/_libs/index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'A'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/jamesabcd/Desktop/TLBX_experiment/pilotZone/expicture/selectAB4.py", line 10, in <module>
    selected_A = random.sample(list(df['A']), 80)
                                    ~~^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/frame.py", line 3761, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 3654, in get_loc
    raise KeyError(key) from err
KeyError: 'A'

我试过print(random.sample(list(df ['A']))),但它不起作用。

jamesabcd@PodeMacBook-Pro Desktop % /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 /Users/jamesabcd/Desktop/TLBX_exp
eriment/pilotZone/expicture/selectAB4.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 3652, in get_loc
    return self._engine.get_loc(casted_key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pandas/_libs/index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'A'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/jamesabcd/Desktop/TLBX_experiment/pilotZone/expicture/selectAB4.py", line 10, in <module>
    selected_A = random.sample(list(df['A']), 80)
                                    ~~^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/frame.py", line 3761, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 3654, in get_loc
    raise KeyError(key) from err
KeyError: 'A'
ulmd4ohb

ulmd4ohb1#

感谢@stressed,解决方案非常简单😀:给列一个名字可以解决这个问题:
列前:

列(命名)后:

相关问题