pandas 和Python数据框和条件移位函数 [英] Pandas and Python Dataframes and Conditional Shift Function

查看:47
本文介绍了 pandas 和Python数据框和条件移位函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据帧中是否有条件的移位"参数?

Is there a conditional "shift" parameter in data frames?

例如,

假设我拥有一辆二手车,并且我有以下数据

Assume I own a used car lot and I have data as follows

SaleDate    Car
12/1/2016   Wrangler
12/2/2016   Camry
12/3/2016   Wrangler
12/7/2016   Prius
12/10/2016  Prius
12/12/2016  Wrangler

我想从此列表中找出两件事-

I want to find two things out from this list -

1)对于每笔交易,汽车的最后销售日期是什么时候?这在Pandas中很简单,只需进行以下简单的转换

1) For each sale, when was the last day that a car was sold? This is simple in Pandas, just a simple shift as follows

df['PriorSaleDate'] = df['SaleDate'].shift()

2)对于每次销售,同一类型的汽车的销售日期是什么时候?因此,例如,在12/3的Wrangler促销会将两行指向12/1(最后一次,第3行中的"car"值等于上一行中的"car"值).

2) For each sale, when was the prior date that the same type of car was sold? So, for example, the Wrangler sale on 12/3 would point two rows back to 12/1 (the last time the "car" value in row 3 was equal to the "car" value in a prior row).

对于12/12售出的牧马人,我希望其值为12/3

For the Wrangler sold on 12/12, I would want the value of 12/3

是否存在一个条件移位参数,该参数可让我获得该行中df ['Car']的值等于该行中df ['Car']的值?

Is there a conditional shift parameter that would allow me to get the row there the value df['Car'] equals the value of df['Car'] in that row?

非常感谢您的帮助

推荐答案

您可以使用groupbyshift():

import io
import pandas as pd

text = """SaleDate    Car
12/1/2016   Wrangler
12/2/2016   Camry
12/3/2016   Wrangler
12/7/2016   Prius
12/10/2016  Prius
12/12/2016  Wrangler"""

df = pd.read_csv(io.StringIO(text), delim_whitespace=True, parse_dates=[0])
df["lastSaleDate"] = df.groupby("Car").SaleDate.shift()

输出:

    SaleDate       Car lastSaleDate
0 2016-12-01  Wrangler          NaT
1 2016-12-02     Camry          NaT
2 2016-12-03  Wrangler   2016-12-01
3 2016-12-07     Prius          NaT
4 2016-12-10     Prius   2016-12-07
5 2016-12-12  Wrangler   2016-12-03

这篇关于 pandas 和Python数据框和条件移位函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆