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.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
ry jin
  • 1
  • 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. – Shashwat Sep 12 '21 at 03:17
  • 1
    This might be helpful: [How do I compare a Unicode string that has different bytes, but the same value?](https://stackoverflow.com/q/49662585/354577) – ChrisGPT was on strike Sep 17 '21 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. – Henry Ecker Dec 16 '21 at 01:15

0 Answers0