0
df = pd.read_excel(excelName)               
df.columns=df.columns.str.strip()
print(df.columns.tolist())
# print result is ['序号', '学号', '姓名', '文件位置', '邮箱地址']
print(df.loc['文件位置'])

It seems I have the column named '文件位置' But when I locate this column by loc, it raises key error.

3
  • Hey, check the exact column name in excel. Sometimes, these columns appear slightly different on IDE Or if you could share the link of the excel. Commented Sep 12, 2021 at 3:17
  • 1
    This might be helpful: How do I compare a Unicode string that has different bytes, but the same value? Commented Sep 17, 2021 at 20:07
  • A very late reply, but loc allows indexing in both dimensions. The format is df.loc[row_indexer, col_indexer]. df['文件位置'] would be equivalent to df.loc[:, '文件位置'] not df.loc['文件位置'] which is looking for a row labeled '文件位置' not a column. This is covered in the accepted answer of the linked duplicate. Commented Dec 16, 2021 at 1:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.