【问题标题】:pandas dataframe how to shift rows based on date熊猫数据框如何根据日期移动行
【发布时间】:2020-08-18 19:34:44
【问题描述】:

我正在尝试评估促销活动对我们客户的影响。目标是从提供促销的那一刻开始评估收入。然而,在不同的地点为不同的客户提供了促销。如何将数据重新排列为Month 0Month 1Month 2Month 3Month 0 是客户第一次获得促销的月份。

【问题讨论】:

  • 欢迎来到 Stackoverflow。你试过什么了?请在您的问题中添加您的尝试。

标签: pandas dataframe python-datetime


【解决方案1】:

使用以下不言自明的代码,您可以获得所需的输出:

# Create DataFrame
import pandas as pd
df = pd.DataFrame({"Account":[1,2,3,4,5,6],\
"May-18":[181,166,221,158,210,159],\
"Jun-18":[178,222,230,189,219,200],\
"Jul-18":[184,207,175,167,201,204],\
"Aug-18":[161,174,178,233,223,204],\
"Sep-18":[218,209,165,165,204,225],\
"Oct-18":[199,206,205,196,212,205],\
"Nov-18":[231,196,189,218,234,235],\
"Dec-18":[173,178,189,218,234,205],\
"Promotion Month":["Sep-18","Aug-18","Jul-18","May-18","Aug-18","Jun-18"]})
df = df.set_index("Account")
cols = ["May-18","Jun-18","Jul-18","Aug-18","Sep-18","Oct-18","Nov-18","Dec-18","Promotion Month"]
df = df[cols]

# Define function to select the four months after promotion
def selectMonths(row):
    cols = df.columns.to_list()
    colMonth0 = cols.index(row["Promotion Month"])
    colsOut = cols[colMonth0:colMonth0+4]
    out = pd.Series(row[colsOut].to_list())
    return out

# Apply the function and set the index and columns of output DataFrame
out = df.apply(selectMonths, axis=1)
out.index = df.index
out.columns=["Month 0","Month 1","Month 2","Month 3"]

那么你得到的输出是:

>>> out
         Month 0  Month 1  Month 2  Month 3
Account                                    
1            218      199      231      173
2            174      209      206      196
3            175      178      165      205
4            158      189      167      233
5            223      204      212      234
6            200      204      204      225

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 2013-06-09
    • 1970-01-01
    • 2017-11-03
    • 2017-08-09
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多