'Unable to read a column of an excel by Column Name using Pandas

Excel Sheet

I want to read values of the column 'Site Name' but in this sheet, the location of this tab is not fixed. I tried,

df = pd.read_excel('TestFile.xlsx', sheet_name='List of problematic Sites', usecols=['Site Name'])

but got value error,

ValueError: Usecols do not match columns, columns expected but not found: ['RBS Name']

The output should be, List of RBS=['TestSite1', 'TestSite2',........]



Solution 1:[1]

try reading the excel columns by this

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

df = pd.read_excel('File.xlsx', sheetname='Sheet1')

for i in df.index:
   print(df['Site Name'][i])

Solution 2:[2]

You can first check dataframe without mentioning column name while reading excel file. Then try to read column names.

Code is as below

import pandas as pd
df = pd.read_excel('TestFile.xlsx', sheet_name='List of problematic Sites')

print(df.head)
print(df.columns)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Hovercraft Full Of Eels