Carmen
Carmen

Reputation: 793

Define function to select columns by name, Dataframe Python

I do have a dataframe with 1000 Simulations and I would like to define a function which allows me to select one columns by their names

The dataframe Looks as follows:

SIMULATION     Sim_1     Sim_2     Sim_3     Sim_4     Sim_5     Sim_6     
2016        0.013765  0.012882  0.006664  0.019607  0.010095  0.020073   
2017        0.016176  0.014744  0.009644  0.022152  0.013778  0.018454   
2018        0.018562  0.016773  0.014824  0.020529  0.018414  0.012878   
2019        0.027624  0.009368  0.016195  0.022941  0.025510  0.002396   
2020        0.037297 -0.000171  0.019798  0.021232  0.028369  0.001007   
2021        0.040634 -0.002396  0.024508  0.018794  0.027421  0.007584

To select one columns one could type

df.Sim_1

This would give me the first column

Is there a possibilty to define a function? I have tried

def select(data, sim_nr):
    sim = data.sim_nr()
    return(sim)

But applying this function to a dataset gives me an error

select(f("I:/tools/python/ESG/yield_RN_FY15_mitVA_BC.FAC",20), "Sim_1")

Any ideas?

Upvotes: 2

Views: 2013

Answers (2)

Zeugma
Zeugma

Reputation: 32095

Use the square bracket selection and you even don't need a function:

df['Sim_1']

Upvotes: 2

jezrael
jezrael

Reputation: 862611

You need [] for selecting column:

def select(data, sim_nr):
    sim = data[sim_nr]
    return(sim)

Upvotes: 4

Related Questions