Variable as Column Name in Unique()

I frequently have to split excel files into different sheets based on the unique values of a column. I’d like to write a little python script to handle this for me.

My intention is to use unique() from Pandas however I want to be able to pass the column name as a variable so that script is robust for any column name.

See the following example:

fn = 'PriceStats.xlsx' #name of file
splitName = 'ProductCategoryName' #Variable to hold column name
master_data = pd.read_excel(fn)
cert_types = master_data.splitName.unique()

The code errors out with: AttributeError: ‘DataFrame’ object has no attribute ‘splitName’

Is what I am trying to accomplish possible with unique()?

Answer

You have to do it this way:

fn = 'PriceStats.xlsx' #name of file
splitName = 'ProductCategoryName' #Variable to hold column name
master_data = pd.read_excel(fn)
cert_types = master_data[splitName].unique()