Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用字符串函数arg命名DF中的新功能_Python_Pandas_Feature Engineering - Fatal编程技术网

Python 使用字符串函数arg命名DF中的新功能

Python 使用字符串函数arg命名DF中的新功能,python,pandas,feature-engineering,Python,Pandas,Feature Engineering,我正在尝试编写一个python函数,它将允许我为机器学习添加功能。我想我误解了在python函数中如何使用字符串 函数查看df的一行,检查行标识符在未来几个月内(下面的行数)是否具有相同的标识符。如果是,则将未来行的“开始”功能的值添加到新功能列,否则将添加初始行的“结束”功能。这是一个定制的换档功能 一旦我添加了这个特性,我想再添加一列1或0,作为带有适当列标签的df的新特性。这将被标记为“壮举”\u如此多个月”\u未来”\u是更高还是更低” 问题是,我甚至无法获得阈值部分周围的第二个二进制文

我正在尝试编写一个python函数,它将允许我为机器学习添加功能。我想我误解了在python函数中如何使用字符串

函数查看df的一行,检查行标识符在未来几个月内(下面的行数)是否具有相同的标识符。如果是,则将未来行的“开始”功能的值添加到新功能列,否则将添加初始行的“结束”功能。这是一个定制的换档功能

一旦我添加了这个特性,我想再添加一列1或0,作为带有适当列标签的df的新特性。这将被标记为“壮举”\u如此多个月”\u未来”\u是更高还是更低”

问题是,我甚至无法获得阈值部分周围的第二个二进制文件。我在添加第一个具有适当名称的新功能时遇到问题

def binary_up_down(name_of_new_feature, months_in_future, percent_threshold):
    name_of_new_feature = [] 
    for i in range(0, df.shape[0], 1): 
        try:
            if df['identifier'][i]==df['identifier'][i + months_in_future]:
                name_of_new_feature.append(df['start'][i + months_in_future])
            else:
                name_of_new_feature.append(df['end'][i])
        except KeyError:
                name_of_new_feature.append(df['end'][i])

    df[str(name_of_new_feature)]=name_of_new_feature

    ### Add test to check if shifted value is above or below threshold and name new feature  
        appropriately ###

    return df
我的想法是如下调用函数:

binary_up_down('feat_value_in_1m', 1, 5)
#Then
binary_up_down('feat_value_in_3m', 3, 5) # and on an on...
当我运行代码时,这一行似乎是问题所在:

df[str(name_of_new_feature)] = name_of_new_feature
…因为它将所有新要素列值添加为列名


非常感谢任何指点

您正在用函数第一行中的列表替换新功能的名称。我建议将其重命名为新功能的
value\u

def binary_up_down(name_of_new_feature, months_in_future, percent_threshold):
    value_of_new_feature = [] 
    for i in range(0, df.shape[0], 1): 
        try:
            if df['identifier'][i]==df['identifier'][i + months_in_future]:
                value_of_new_feature .append(df['start'][i + months_in_future])
            else:
                value_of_new_feature .append(df['end'][i])
        except KeyError:
                value_of_new_feature .append(df['end'][i])

    df[name_of_new_feature]=value_of_new_feature 

    ### Add test to check if shifted value is above or below threshold and name new feature  
        appropriately ###

    return df