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

Why this upper column dataframe function doesn't work?

I want to create a function to upper columns, I already try this:

def upperColumn(DataframeColumn):
  DataframeColumn = DataframeColumn.str.upper()

upperColumn(df['ANYTHING'])

I need this to be the same as:

df['ANYTHING'] = df['ANYTHING'].str.upper()

obs. Pandas is imported and the dataframe is created already.

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

Thank you in advance.

>Solution :

You can’t quite do this because DataframeColumn a parameter to the function, so it’s basically a variable local to the function. Therefore, when you overwrite it inside the function, it won’t actually change the column’s value.

Instead, you can pass the dataframe and the column name as separate parameters:

def upperColumn(Dataframe, Column):
  Dataframe[Column] = Dataframe[Column].str.upper()

upperColumn(df, 'ANYTHING')
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