如何在功能中添加后缀?

发布于 2025-02-13 07:00:53 字数 350 浏览 1 评论 0原文

我想创建一个预处理功能,该功能将在数据框架名称中添加后缀。

这将使我可以轻松地识别我的数据范围,而无需或使用功能缩放和使用的技术(如果适用)。

例如:

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")]

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")]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2025-02-20 07:00:53

在数据框架名称中添加后缀。

这将使我轻松识别

其中包含函数内部功能的变量名称,请考虑

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

导致

NameError: name 'y' is not defined

您可以选择仅设置pandas.dataframe不是方法或列的名称,

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

例如然后以正常方式使用实例属性访问

print(df.comment)  # example dataframe

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
没有心的人 2025-02-20 07:00:53

使用add_suffix解决此问题:

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

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文