如何在不同的输入参数中应用相同的函数在pandas数据框中创建新列? [英] How to apply the same function with different input arguments to create new columns in pandas dataframe?

查看:57
本文介绍了如何在不同的输入参数中应用相同的函数在pandas数据框中创建新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个示例数据框:

So i've this sample dataframe:

      x_mean    x_min    x_max     y_mean     y_min     y_max
 1      85.6        3      264       75.7         3       240
 2     105.5        6      243       76.4         3       191
 3      95.8       19      287       48.4         8       134
 4      85.5       50      166       64.8        32       103
 5      55.9       24      117       46.7        19        77 


x_range = [list(range(0,50)),list(range(51,100)),list(range(101,250)),list(range(251,350)),list(range(351,430)),list(range(431,1000))]
y_range = [list(range(0,30)),list(range(31,60)),list(range(61,90)),list(range(91,120)),list(range(121,250)),list(range(251,2000))]


#here x = Any column with mean value (eg. x_mean or y_mean)
# y = x_range / y_range 

def min_max_range(x,y):
for a in y:
    if int(x) in a:
        min_val = min(a)
        max_val = max(a)+1
        return max_val - min_val

def min_range(x,y):
for a in y:
    if int(x) in a:
        min_val = min(a)
        return min_val

现在我想将这些功能min_max_range()min_range()应用于列x_mean, y_mean以获取新列.

Now i want to apply these function min_max_range() and min_range() to column x_mean, y_mean to get new columns.

类似于功能min_max_val使用列x_mean&范围x_range作为创建列x_min_max_val的输入,类似地,列y_mean&范围y_range用于列y_min_max_val:

Like the function min_max_val is using column x_mean & the range x_range as the input to create column x_min_max_val , similarly column y_mean & the range y_range are used for the column y_min_max_val :

我可以使用这一种衬管一个接一个地创建每一列,但是我想一次将它应用于两个列x_mean & y_mean列.

I can create each column one by one, by using these one liners, but i want to apply this to both column x_mean & y_mean columns in one go with a one liner.

df['x_min_max_val'] = df['x_mean'].apply(lambda x: min_max_range(x,x_range))
df['y_min_max_val'] = df['y_mean'].apply(lambda x: min_max_range(x,y_range))  

结果数据框应如下所示:

The resultant dataframe should look like this:

      x_mean    x_min    x_max     y_mean     y_min     y_max    x_min_max_val   y_min_max_val        x_min_val   y_min_val
1      85.6        3      264       75.7         3       240                49              29               51          61
2     105.5        6      243       76.4         3       191               149              29              101          91
3      95.8       19      287       48.4         8       134                49              29               51          91
4      85.5       50      166       64.8        32       103                49              29               51          61
5      55.9       24      117       46.7        19        77                49              29               51          31

我想一次创建这些列,而不是一次创建一列.我怎样才能做到这一点?有什么建议?或类似的东西可以工作?

I want to create these columns in one go, instead of creating one column ata time. How can i do this? Any suggestions? or something like this could work?

df.filter(regex='mean').apply(lambda x: min_max_range(x,x+'_range'))

推荐答案

这是您需要遵循的概念才能实现这一目标.首先,您需要将范围存储在字典中,以允许通过名称访问它们.

This is the concept that you need to follow to make this happen. First you need to have your ranges stored in a dictionary to enable access to them through names.

range_dict = {}
range_dict['x_range'] = x_range
range_dict['y_range'] = y_range

此外,您需要在列表中具有进行计算所需的列(或者,如果它们具有特定的模式,则可以使用regex来获取这些列)

Also, you need to have the columns that you need to do the calculation for in a list (or you can use regex to get those if they have a specific pattern)

mean_cols_list = ['x_mean', 'y_mean']

现在,要将您的函数应用于所有列,您需要定义一个这样的函数

Now, to apply your function over all columns, you need to define a function like this

def min_max_calculator(df, range_dictionary, mean_columns_list):
    for i in range(len(mean_cols_list)):
        # this returns 'x_mean'
        current_column = mean_cols_list[i]
        # this returns 'x_min_max_value'
        output_col_name = current_column.replace('mean','min_max_value')
        # this returns 'x_range'
        range_name = current_column.replace('mean','range')
        # this returns the list of ranges for x_range
        range_list = range_dict[range_name]
        # This add the calculated column to the dataframe
        df[output_col_name] = df[current_column].apply(lambda x: min_max_range(x,range_list))
    return(df)

df_output = min_max_calculator(df, range_dict, mean_cols_list)

这篇关于如何在不同的输入参数中应用相同的函数在pandas数据框中创建新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆