如何在Pandas.DataFrame中的列上进行迭代并将函数的结果附加到同一行?

时间:2019-10-15 04:42:46

标签: python pandas csv dataframe datetime

我有一个通过以下CSV生成的Pandas.DataFrame

Category,Brand,Product Name,Price,Expiration Date, Package ID,Quantity
Cat1,Brand1,Product1,$1000,07/14/2020,XXXXXX,34

我正在尝试在CSV后面添加一列,每行中都有一个整数,对应于到期日期有多短(4表示大于6个月,3表示在3到6个月之间等)。

我的问题是,当尝试将Expiration Date列转换为日期时间(使用pandas.to_datetime(df['Expiration Date']))并应用我的classify_expiration()函数时,类型要么与函数指示的类型不匹配,要么它尝试将函数应用于index 0,我认为这是标题(因此与%m/%d/%Y格式不匹配)。我尝试过在分类函数内以及在.apply()调用之前将其转换为datetime。我还尝试过使用timedelta来比较到期日期和今天的当前日期,但是不适用于datetime.date.today()

这是我尝试的第一种方法:

def classify_expiration(row):    
    one_week = timedelta(weeks=1, days=0, hours=0, minutes=0, seconds=0)

    if ((one_week * 0) <= (date.today() - row['Expiration Date']) <= (one_week * 4)):
        return 4

这种方式给我带来了与类型错误有关的错误,这些类型在index 0上不正确,或者无法将功能应用于系列。

这是我刚刚尝试过的给我AssertionError的内容:

def days_between(date1, date2):
    """Calculates the number of days between two dates

    Keyword arguments:
    date1 -- The first date in the subtraction.
    date2 -- The second date in the subtraction.
    """
    date1 = datetime.strptime(date1, '%m/%d/%Y')
    date2 = datetime.strptime(date2, '%m/%d/%Y')
    return abs((date2 - date1).days)


def classify_expiration(row):
    """Calculate days/weeks to expiration. Assign quartile based on value.

    Keyword arguments:
    row -- row in a `pandas.core.frame.DataFrame` object. e.g. `df['A']`
    """

    date_today = datetime.strptime(
        date.today().strftime('%m/%d/%Y'), '%m/%d/%Y')

    if (days_between(row, date_today) <= 30):
        return 4
    if (31 <= days_between(row, date_today) <= 90):
        return 3
    if (91 <= days_between(row, date_today) <= 120):
        return 2
    if (days_between(row, date_today) >= 121):
        return 1

在这里我尝试应用该功能:

# Convert column to `datetime` if its current type is str
pd.to_datetime(product_sales['Expiration Date'])

# Applying the `classify_expiration()` function
product_sales['Expiration Quartile'] = product_sales.apply(
    lambda row: classify_expiration(row), axis=1
)

我希望函数向DataFrame追加一个新列,该列包含针对每一行的到期日期生成的四分位数。我将收到AssertionErrorargument 1 must be str, not Series等错误,以及与index 0相关的其他错误。

1 个答案:

答案 0 :(得分:1)

如果分配回days_between,然后使用product_sales['Expiration Date'] = pd.to_datetime(product_sales['Expiration Date'])进行标量循环,则需要在product_sales['Expiration Date'].apply(classify_expiration)函数中删除转换为日期时间:

def days_between(date1, date2):
    """Calculates the number of days between two dates

    Keyword arguments:
    date1 -- The first date in the subtraction.
    date2 -- The second date in the subtraction.
    """
    return abs((date2 - date1).days)


product_sales['Expiration Date'] = pd.to_datetime(product_sales['Expiration Date'])

product_sales['Expiration Quartile'] = (product_sales['Expiration Date']
                                               .apply(classify_expiration))
print (product_sales)
  Category   Brand Product Name  Price Expiration Date Package ID  Quantity  \
0     Cat1  Brand1     Product1  $1000      2020-07-14     XXXXXX        34   

   Expiration Quartile  
0                    1  

Pandas对binnig具有特殊功能,因此可以使用cut

product_sales['Expiration Date'] = pd.to_datetime(product_sales['Expiration Date'])

product_sales['Expiration Quartile'] = (product_sales['Expiration Date']
                                             .apply(classify_expiration))

s = product_sales['Expiration Date'].sub(pd.to_datetime('today').floor('d')).dt.days

product_sales['Expiration Quartile1'] = pd.cut(s, 
                                               bins=[0, 30, 90,120, np.inf], 
                                               labels=[4,3,2,1])
print (product_sales)
  Category   Brand Product Name  Price Expiration Date Package ID  Quantity  \
0     Cat1  Brand1     Product1  $1000      2020-07-14     XXXXXX        34   
1     Cat1  Brand1     Product1  $1000      2020-01-13     XXXXXX        34   
2     Cat1  Brand1     Product1  $1000      2019-11-01     XXXXXX        34   
3     Cat1  Brand1     Product1  $1000      2020-01-15     XXXXXX        34   

   Expiration Quartile Expiration Quartile1  
0                    1                    1  
1                    3                    3  
2                    4                    4  
3                    2                    2