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.
Thank you in advance.
CodePudding user response:
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')