简体   繁体   English

返回以pandas数据框为参数的函数的输出

[英]Return output of function that takes pandas dataframe as a parameter

I have a pandas dataframe that looks like: 我有一个熊猫数据框,看起来像:

d = {'some_col' : ['A', 'B', 'C', 'D', 'E'],
     'alert_status' : [1, 2, 0, 0, 5]}
df = pd.DataFrame(d)

Quite a few tasks at my job require the same tasks in pandas. 我的工作中有很多任务需要在熊猫中执行相同的任务。 I'm beginning to write standardized functions that will take a dataframe as a parameter and return something. 我开始写标准化的函数,这些函数将数据框作为参数并返回一些信息。 Here's a simple one: 这是一个简单的例子:

def alert_read_text(df, alert_status=None):
    if (alert_status is None):
        print 'Warning: A column name with the alerts must be specified'
    alert_read_criteria = df[alert_status] >= 1
    df[alert_status].loc[alert_read_criteria] = 1
    alert_status_dict = {0 : 'Not Read',
                         1 : 'Read'}
    df[alert_status] = df[alert_status].map(alert_status_dict)
    return df[alert_status]

I'm looking to have the function return a series. 我希望该函数返回一个序列。 This way, one could add a column to an existing data frame: 这样,可以在现有数据框中添加一列:

df['alert_status_text'] = alert_read_text(df, alert_status='alert_status')

However, currently, this function will correctly return a series, but also modifies the existing column. 但是,当前,此函数将正确返回一个序列,但也会修改现有列。 How do you make it so the original column passed in does not get modified? 如何使传入的原始列不被修改?

As you've discovered the passed in dataframe will be modified as params are passed by reference, this is true in python and nothing to do with pandas as such. 正如您所发现的那样,传入的数据帧将随着通过引用传递的参数而被修改,这在python中是正确的,因此与大熊猫无关。

So if you don't want to modify the passed df then take a copy: 因此,如果您不想修改传递的df,请复制一份:

def alert_read_text(df, alert_status=None):
    if (alert_status is None):
        print 'Warning: A column name with the alerts must be specified'
    copy = df.copy()
    alert_read_criteria = copy[alert_status] >= 1
    copy[alert_status].loc[alert_read_criteria] = 1
    alert_status_dict = {0 : 'Not Read',
                         1 : 'Read'}
    copy[alert_status] = copy[alert_status].map(alert_status_dict)
    return copy[alert_status]

Also see related: pandas dataframe, copy by value 另请参阅相关内容: pandas数据框,按值复制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 how to define a function that gets the number of columns in a pandas dataframe that takes the dataframe as the function's parameter - how to define a function that gets the number of columns in a pandas dataframe that takes the dataframe as the function's parameter 类型提示以返回类型为参数的 function - type hinting a function that takes the return type as parameter 将具有常量参数的函数应用于pandas数据帧 - apply function with constant parameter to pandas dataframe pandas DataFrame.drop 函数中的整数参数 - Integer parameter in pandas DataFrame.drop function 将过滤器应用于pandas dataframe作为参数应用于python中的function - Applying filter to pandas dataframe as a parameter to function in python 将函数应用于带有两个参数的pandas数据框中的列 - Apply function to column in pandas dataframe that takes two arguments 如何将需要多个 arguments 的 function 应用到 pandas DataFrame - How to apply a function that takes multiple arguments to a pandas DataFrame Python map()函数输出到Pandas DataFrame中 - Python map() function output into Pandas DataFrame Function 的 pandas DataFrame 创建单个值 Z78E6221F6393D13566681DB398FZ4CE - Function of pandas DataFrame that creates single value output 将“函数输出”写入良好的Pandas数据框 - Write 'Function output ' into a nice Pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM