简体   繁体   English

熊猫日期时间到整数索引

[英]Pandas datetime to integer index

Lets say I have the following data frame:假设我有以下数据框:

d = {'store': [a, a, a, b, b], 'date': [2020-1-30, 2020-1-30, 2020-2-28, 
2020-1-30, 2020-3-30], 'amount': [1, 2, 3, 5, 2]}
df = pd.DataFrame(data=d)
df
    store      date       amount
0     a     2020-1-30       1
1     a     2020-1-30       2
2     a     2020-2-28       3
3     b     2020-1-30       5
4     b     2020-3-30       2

I would like to have a column that is an incrementing integer that specifies what period the dates corresponds to for a specific store, as well aa flag column that notes if the date is the highest date, the output would be the following:我想要一列是一个递增整数,它指定日期对应于特定商店的哪个时期,以及一个标记列,指出日期是否是最高日期,输出如下:

    store      date       amount   period   is_max_period
0     a     2020-1-30       1          1          0
1     a     2020-1-30       2          1          0
2     a     2020-2-28       3          2          1
3     b     2020-1-30       5          1          0
4     b     2020-3-30       2          2          1

Would would be the bets way to go about this?这将是解决这个问题的赌注方式吗?

Try with transform with factorize and max尝试使用factorizemax进行transform

g = df.groupby(['store'])['date']
df['period'] = g.transform(lambda x : x.factorize()[0]+1)
df['is_max_period'] = df.date.eq(g.transform('max')).astype(int)
df
  store       date  amount  period  is_max_period
0     a  2020-1-30       1       1              0
1     a  2020-1-30       2       1              0
2     a  2020-2-28       3       2              1
3     b  2020-1-30       5       1              0
4     b  2020-3-30       2       2              1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM