Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing the name of a pandas dataframe column to a function

I’m trying to write a function that takes the name of a column and splits the dataframe based on the values of that column. I have the following

df_split = df[df.a == 1]

I’m trying to implement the following idea

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def f(df,column_name): 
  df_split = df[df.column_name == 1]

Any help is highly appreciated.

>Solution :

Please change the function to following:

def f(df,column_name): 
  df_split = df[df[column_name] == 1]
  return df_split

df.column_name will work only if the dataframe really have a column labelled as column_name so don’t use it inside the function

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading