[英]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.