0

I have an Excel file :

Test_Case   Value
Case_1      0.988532846
Case_2      0.829241525
Case_3      0.257209267
Case_4      0.871698313
Case_5      0.63913665

with pandas I have seen that we can get a column like this :

import pandas as pd
myExcelFile = "data.xlsx"
readExcelFile = pd.read_excel(myExcelFile, sheet_name=0, index=0)
testCaseColumn = readExceFile.Test_Case

result :

0 Case_1
1 Case_2
2 Case_3
3 Case_4
4 Case_5

The name of the column can be change, and I would like to create a function with two arguments to get the column I want :

def getColumn(readExceFile, columnName):
    return readExceFile.columnName

I would like to know how can I attribute the name of the column to my readExcelFile parameter ?

Thanks for your help

3
  • do you need the usecols parameter in read_excel? check this: stackoverflow.com/questions/26063231/… Commented Dec 18, 2019 at 16:36
  • 1
    It's generally better to access columns using ['label'], not .label. Can you clarify what you're trying to do? Also, variable and function names should follow the lower_case_with_underscores style. Commented Dec 18, 2019 at 16:46
  • @AMC I have to review a code and rewrite it. In the code these lines are written several times. I want to create function to have a clear code. Commented Dec 19, 2019 at 8:54

2 Answers 2

1

You can use getattr.

def getColumn(readExceFile, columnName):
    return getattr(readExceFile, columnName)
Sign up to request clarification or add additional context in comments.

Comments

1

Since your_dataframe.column_name works only with column names w/o space character and you've mentioned that column name can be changed, you can call for column name with your_dataframe.loc[:,'column_name'] (see Alexander Céciles comment).

On the other hand, if your dataset has always the same structure (n-columns, first one with some categorical data, second one with values, etc.) then you can call it also directly with your_dataframe.iloc[:,0], with 0 being your first column-of-interest in the set.

Finally if you really need to have a separate function (besides at least those two I've mentioned) which returns exactly the same output then you may use this:

def get_column(your_dataframe, column_name):
    return your_dataframe.loc[:,column_name]

... what is highly non-pythonic way of writing the code (see Zen of Python)

1 Comment

column name with spaces can be a problem, your_dataframe.iloc[:,0] can be solve this problem. Thanks for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.