将函数应用于数据框中的列列表

huangapple go评论59阅读模式
英文:

Apply function to list of columns from a dataframe

问题

I'm creating a function that accepts 3 inputs: a dataframe, a column, and a list of columns.

import numpy as np

df = pd.DataFrame([[1, 2, 3, 4], [1, 3, 5, 6], [4, 6, 7, 8], [5, 4, 3, 6]], columns=['A', 'B', 'C', 'D'])

def pre_process(dataframe, y_col_name, x_col_names):
    return new_dataframe

The calculation to be applied to y_col_name's rows is each value of y_col_name divided by the mean of y_col_name.

The calculation to be applied to each of the list of columns in x_col_name is each value of each column, divided by the column's standard deviation.

I would like some help to write the function. I think I need to use an "apply" or a "lambda" function but I'm unsure.

This is what calling the command would look like:

pre_process_data = pre_process(df, 'A', ['B', 'D'])

Thanks

英文:

I'm creating a function that accepts 3 inputs: a dataframe, a column and a list of columns.
The function should apply a short calculation to the single column, and a different short calculation to the list of other columns. It should return a dataframe containing just the amended columns (and their amended rows) from the original dataframe.

import numpy as np

df = pd.DataFrame([[1, 2, 3, 4], [1, 3, 5, 6], [4, 6, 7, 8], [5, 4, 3, 6], columns=['A', 'B', 'C', 'D'])

def pre_process(dataframe, y_col_name, x_col_names):
    return = new_dataframe

The calculation to be applied to y_col_name's rows is each value of y_col_name divided by the mean of y_col_name.

The calculation to be applied to each of the list of columns in x_col_name is each value of each column, divided by the column's standard deviation.

I would like some help to write the function. I think I need to use an "apply" or a "lambda" function but I'm unsure.

This is what calling the command would look like:

pre_process_data = preprocess(df,'A', ['B','D'])

Thanks

答案1

得分: 0

def pre_process(dataframe, y_col_name, x_col_names):
    new_dataframe = dataframe.copy()
    new_dataframe[y_col_name] = new_dataframe[y_col_name] / new_dataframe[y_col_name].mean()
    new_dataframe[x_col_names] = new_dataframe[x_col_names] / new_dataframe[x_col_names].std()
    return new_dataframe
英文:
def pre_process(dataframe, y_col_name, x_col_names):
    new_dataframe = dataframe.copy()
    new_dataframe[y_col_name] =  new_dataframe[y_col_name]/new_dataframe[y_col_name].mean()
    new_dataframe[x_col_names] = new_dataframe[x_col_names]/new_dataframe[x_col_names].std()
    return new_dataframe

Is this what you mean?

huangapple
  • 本文由 发表于 2020年1月7日 02:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定