我对R dplyr非常熟悉数据分析,我试图将我在dplyr中编写的一些代码转换为pandas。我的数据包含ID列和DATE标识的人,该人使用某个PRODUCT。我试图找出将以下R dplyr代码转换为python pandas代码的最佳方法。基本上,我按ID列分组,过滤特定类型的产品,然后添加一个列(对于组中的每一行)添加该人的最小(第一次使用)日期和最大(最后使用)日期和产品。最后,我还添加了一个列,用于计算上次使用日期和首次使用日期之间的天数。这是数据:
ID PRODUCT DATE
A ITEM1 1/30/15
B ITEM1 2/23/14
A ITEM2 3/22/15
C ITEM1 1/23/12
B ITEM1 4/12/15
A ITEM3 2/2/14
C ITEM1 1/1/17
A ITEM1 2/20/15
A ITEM1 5/18/15
使用dplyr我可以做到
library(dplyr)
library(lubridate)
df <- df %>%
mutate(DATE = mdy(DATE)) %>%
group_by(ID) %>%
filter(PRODUCT == "Item1") %>%
mutate(FIRST = min(DATE), LAST = max(DATE), DAYS = LAST - FIRST)
给了我
ID PRODUCT DATE FIRST LAST DAYS
(fctr) (fctr) (time) (time) (time) (dfft)
1 A ITEM1 2015-01-30 2015-01-30 2015-05-18 108 days
2 B ITEM1 2014-02-23 2014-02-23 2015-04-12 413 days
3 C ITEM1 2012-01-23 2012-01-23 2017-01-01 1805 days
4 B ITEM1 2015-04-12 2014-02-23 2015-04-12 413 days
5 C ITEM1 2017-01-01 2012-01-23 2017-01-01 1805 days
6 A ITEM1 2015-02-20 2015-01-30 2015-05-18 108 days
7 A ITEM1 2015-05-18 2015-01-30 2015-05-18 108 days
数据:
df <- structure(list(ID = structure(c(1L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 1L), .Label = c("A", "B", "C"), class = "factor"),
PRODUCT = structure(c(1L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 1L), .Label = c("ITEM1", "ITEM2", "ITEM3"), class = "factor"),
DATE = structure(c(3L, 6L, 7L, 2L, 8L, 4L, 1L, 5L, 9L),
.Label = c("1/1/17", "1/23/12", "1/30/15", "2/2/14", "2/20/15", "2/23/14", "3/22/15", "4/12/15", "5/18/15"),
class = "factor")),
.Names = c("ID", "PRODUCT", "DATE"), class = "data.frame", row.names = c(NA, -9L))
我怎样才能在熊猫中做同样的事情?
答案 0 :(得分:3)
使用agg
+ groupby
funcs = dict(FIRST='min', LAST='max', DAYS=np.ptp)
d1 = df.join(df.groupby(['ID', 'PRODUCT']).DATE.agg(funcs), on=['ID', 'PRODUCT'])
答案 1 :(得分:0)
使用 datar
将您的 R 代码转换为 Python 非常容易:
>>> from datar.all import f, tribble, as_date, group_by, mutate, min, max, filter
[2021-06-24 13:44:46][datar][WARNING] Builtin name "min" has been overriden by datar.
[2021-06-24 13:44:46][datar][WARNING] Builtin name "max" has been overriden by datar.
[2021-06-24 13:44:46][datar][WARNING] Builtin name "filter" has been overriden by datar.
>>>
>>> df = tribble(
... f.ID, f.PRODUCT, f.DATE,
... "A", "ITEM1", "1/30/15",
... "B", "ITEM1", "2/23/14",
... "A", "ITEM2", "3/22/15",
... "C", "ITEM1", "1/23/12",
... "B", "ITEM1", "4/12/15",
... "A", "ITEM3", "2/2/14",
... "C", "ITEM1", "1/1/17",
... "A", "ITEM1", "2/20/15",
... "A", "ITEM1", "5/18/15",
... )
>>> df
ID PRODUCT DATE
<object> <object> <object>
0 A ITEM1 1/30/15
1 B ITEM1 2/23/14
2 A ITEM2 3/22/15
3 C ITEM1 1/23/12
4 B ITEM1 4/12/15
5 A ITEM3 2/2/14
6 C ITEM1 1/1/17
7 A ITEM1 2/20/15
8 A ITEM1 5/18/15
>>> df >> mutate(
... DATE=as_date(f.DATE, "%m/%d/%y")
... ) >> group_by(
... f.ID
... ) >> filter(
... f.PRODUCT == "ITEM1"
... ) >> mutate(
... FIRST=min(f.DATE),
... LAST=max(f.DATE),
... DAYS=f.LAST - f.FIRST
... )
ID PRODUCT DATE FIRST LAST DAYS
<object> <object> <object> <object> <object> <timedelta64[ns]>
0 A ITEM1 2015-01-30 2015-01-30 2015-05-18 108 days
1 B ITEM1 2014-02-23 2014-02-23 2015-04-12 413 days
2 C ITEM1 2012-01-23 2012-01-23 2017-01-01 1805 days
3 B ITEM1 2015-04-12 2014-02-23 2015-04-12 413 days
4 C ITEM1 2017-01-01 2012-01-23 2017-01-01 1805 days
5 A ITEM1 2015-02-20 2015-01-30 2015-05-18 108 days
6 A ITEM1 2015-05-18 2015-01-30 2015-05-18 108 days
[Groups: ID (n=3)]
免责声明:我是 datar
软件包的作者。