0

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")]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ElMeTeOr
  • 55
  • 10

2 Answers2

1

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')
  • This will add a suffix to the column names not to the dataframe name. – ElMeTeOr Jul 06 '22 at 14:11
  • 1
    Oh my bad. I think you will find your solution here: https://stackoverflow.com/questions/20237454/add-prefix-suffix-to-dataframe-name-or-pass-parameter-into-dataframe-name-in – Mohammad Nuseirat Jul 06 '22 at 14:15
1

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
Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Yes you are right, that makes sense, might as well create a copy of the df with the suffix added before using the function. The idea of ​​the comment is very judicious, thank you. – ElMeTeOr Jul 06 '22 at 14:16