Home > Software design >  How to add a sufix to dataframe in a function?
How to add a sufix to dataframe in a function?

Time:07-07

I want to create a preprocessing function that would add a suffix to the dataframe name.

This will allow me to easily identify my dataframes without or with feature scaling and the technique used if applicable.

For example:

mas = MaxAbsScaler()


def preproce_MaxAbsScaler(df):
    [df str("_mas")]=pd.DataFrame(mas.fit_transform(df),
                           index=df.index,
                           columns=df.columns)
    return [df str("_mas")]

CodePudding user response:

add a suffix to the dataframe name.

This will allow me to easily identify

Variables names inside function are contained therein, consider that

def func(x):
    y = x   1
    return y
func(42)
print(y)

lead to

NameError: name 'y' is not defined

You might elect to just set attribute of pandas.DataFrame which is not name of method or column thereof, for example

import pandas as pd
df = pd.DataFrame({'X':[1],'Y':[2],'Z':[3]})
df.comment = "example dataframe"

and then access in normal way of using instance attributes

print(df.comment)  # example dataframe

CodePudding user response:

Use add_suffix to solve this issue:

#Example:
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [3, 4, 5, 6]})
df.add_suffix('_MyDataFrame')
  • Related