将功能应用于数据框列

时间:2020-05-07 07:52:57

标签: python pandas function dataframe apply

我有数据框x,

Please view the x dataframe here

我们想使用下面的函数创建新列,该函数将在开始处添加Complete columns值并创建新列结束。

import datetime
def date_by_adding_business_days(from_date, add_days):
    business_days_to_add = add_days
    current_date = from_date
    while business_days_to_add > 0:
        current_date += datetime.timedelta(days=1)
        weekday = current_date.weekday()
        if weekday >= 5: # sunday = 6
            continue
        business_days_to_add -= 1
    return current_date

我已经尝试过以下操作,请帮忙。

x['Finish'] = x.apply(date_by_adding_business_days(datetime.date.today(), x['Complete']))

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

3 个答案:

答案 0 :(得分:2)

尝试重构您的代码。如果仅将功能应用到一列,则您做错了。此外,由于某种原因,您试图调用函数传递时间。为什么不能在函数中正确设置呢?

import datetime
def date_by_adding_business_days(add_days):
    business_days_to_add = add_days
    current_date = datetime.date.today()
    while business_days_to_add > 0:
        current_date += datetime.timedelta(days=1)
        weekday = current_date.weekday()
        if weekday >= 5: # sunday = 6
            continue
        business_days_to_add -= 1
    return current_date

x['Finish'] = x['Complete'].apply(date_by_adding_business_days)

答案 1 :(得分:2)

您的方法是正确的。不过,您只需要传递函数引用即可。

致电时申请。它将数据框行传递给函数并调用它。

您可以在函数本身中计算像今天这样的变量

def date_by_adding_business_days(row):
    add_days = row['Complete']
    from_date = datetime.date.today()

    business_days_to_add = add_days
    current_date = from_date
    while business_days_to_add > 0:
        current_date += datetime.timedelta(days=1)
        weekday = current_date.weekday()
        if weekday >= 5:  # sunday = 6
            continue
        business_days_to_add -= 1
    return current_date

x['Finish'] = x.apply(date_by_adding_business_days, axis=1)

答案 2 :(得分:0)

尽管以上两种方法都达到了目的,但我认为在对单个列的行执行apply()的情况下,第二种方法更快。如果您根据问题中提供的示例数据框在笔记本中安排这些解决方案的时间,则差异似乎很明显。

随附为屏幕截图。 Timing apply() function