- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
更新:从版本 0.20.0 开始,pandas cut/qcut 确实可以处理日期字段。参见 What's New了解更多。
pd.cut and pd.qcut now support datetime64 and timedelta64 dtypes (GH14714, GH14798)
原始问题:Pandas cut 和 qcut 函数非常适合“分桶”连续数据以用于数据透视表等,但我看不到在混合。令人沮丧,因为 pandas 在所有与时间相关的事情上都非常出色!
这是一个简单的例子:
def randomDates(size, start=134e7, end=137e7):
return np.array(np.random.randint(start, end, size), dtype='datetime64[s]')
df = pd.DataFrame({'ship' : randomDates(10), 'recd' : randomDates(10),
'qty' : np.random.randint(0,10,10), 'price' : 100*np.random.random(10)})
df
price qty recd ship
0 14.723510 3 2012-11-30 19:32:27 2013-03-08 23:10:12
1 53.535143 2 2012-07-25 14:26:45 2012-10-01 11:06:39
2 85.278743 7 2012-12-07 22:24:20 2013-02-26 10:23:20
3 35.940935 8 2013-04-18 13:49:43 2013-03-29 21:19:26
4 54.218896 8 2013-01-03 09:00:15 2012-08-08 12:50:41
5 61.404931 9 2013-02-10 19:36:54 2013-02-23 13:14:42
6 28.917693 1 2012-12-13 02:56:40 2012-09-08 21:14:45
7 88.440408 8 2013-04-04 22:54:55 2012-07-31 18:11:35
8 77.329931 7 2012-11-23 00:49:26 2012-12-09 19:27:40
9 46.540859 5 2013-03-13 11:37:59 2013-03-17 20:09:09
要按价格或数量分组,我可以使用 cut/qcut 对它们进行分桶:
df.groupby([pd.cut(df['qty'], bins=[0,1,5,10]), pd.qcut(df['price'],q=3)]).count()
price qty recd ship
qty price
(0, 1] [14.724, 46.541] 1 1 1 1
(1, 5] [14.724, 46.541] 2 2 2 2
(46.541, 61.405] 1 1 1 1
(5, 10] [14.724, 46.541] 1 1 1 1
(46.541, 61.405] 2 2 2 2
(61.405, 88.44] 3 3 3 3
但我看不到任何简单的方法可以对我的“记录”或“发货”日期字段执行相同的操作。例如,生成一个类似的计数表,按(比如说)每月的 recd 和 ship 桶分割。似乎 resample() 拥有所有的机制来分阶段,但我不知道如何在这里应用它。 'date cut' 中的桶(或级别)相当于 pandas.PeriodIndex,然后我想用 df['recd'] 的每个值标记它落入的时间段?
所以我正在寻找的输出类型类似于:
ship recv count
2011-01 2011-01 1
2011-02 3
... ...
2011-02 2011-01 2
2011-02 6
... ... ...
更一般地说,我希望能够在输出中混合和匹配连续变量或分类变量。假设 df 还包含一个带有红色/黄色/绿色值的“状态”列,那么也许我想按状态、价格桶、发货和记录桶汇总计数,所以:
ship recv price status count
2011-01 2011-01 [0-10) green 1
red 4
[10-20) yellow 2
... ... ...
2011-02 [0-10) yellow 3
... ... ... ...
作为奖励问题,修改上面的 groupby() 结果以仅包含一个名为“count”的输出列的最简单方法是什么?
最佳答案
这是一个使用 pandas.PeriodIndex 的解决方案(警告:PeriodIndex 不似乎支持倍数 > 1 的时间规则,例如“4M”)。我认为奖励问题的答案是 .size()
。
In [49]: df.groupby([pd.PeriodIndex(df.recd, freq='Q'),
....: pd.PeriodIndex(df.ship, freq='Q'),
....: pd.cut(df['qty'], bins=[0,5,10]),
....: pd.qcut(df['price'],q=2),
....: ]).size()
Out[49]:
qty price
2012Q2 2013Q1 (0, 5] [2, 5] 1
2012Q3 2013Q1 (5, 10] [2, 5] 1
2012Q4 2012Q3 (5, 10] [2, 5] 1
2013Q1 (0, 5] [2, 5] 1
(5, 10] [2, 5] 1
2013Q1 2012Q3 (0, 5] (5, 8] 1
2013Q1 (5, 10] (5, 8] 2
2013Q2 2012Q4 (0, 5] (5, 8] 1
2013Q2 (0, 5] [2, 5] 1
关于python - pandas 日期字段的 cut/qcut 等价于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16319106/
假设我有一个数据框: import numpy as np import pandas as pd df = pd.DataFrame(np.random.normal(0,1,[100,50]))
我想将 pandas qcut 应用于滚动窗口。我不知道该怎么做...想法是在过去 20 天中,找到属于上四分位数的值,找到上四分位数中的值的平均值。并返回该滚动时间序列的平均值。 如果我有 s =
这是一个简单的数据样本系列: sample Out[2]: 0 0.047515 1 0.026392 2 0.024652 3 0.022854 4 0.020397
我创建了一个分类变量,我想为其他变量的特定值创建一个新类别 我有一个带有变量 Score 的数据框,其值介于 0-100 之间。我做了十分之一,但我想为特定值创建一个新类别 df['Score_pr'
考虑以下数据: s = pd.Series([1, 1, 1, 2]) pd.qcut(s, 2, labels=False) 我想要等概率组,即 [0, 0, 1, 1]。相反,qcut 可以引发异
在此处的 Pandas 笔记本上 http://nbviewer.ipython.org/urls/raw.github.com/carljv/Will_it_Python/master/ARM/ch
我有一个数据框,我可以从中选择一个列(系列),如下所示: df: value_rank 275488 90 275490 35 275491
Pandas docs关于 qcut 函数有这样的说法: Discretize variable into equal-sized buckets based on rank or based on
我使用 pandas.qcut 将数据分为 5 组,并希望根据 qcut 的最小和最大分数来标记每个组。 例如,我尝试使用数据框列中的“年龄”数据。 df['age group'] = pd.qcut
我正在慢慢地从 R 转向 python + pandas,我面临着一个我无法解决的问题...... 我需要离散化一列中的值,方法是将它们分配给 bin,并将具有这些 bin 名称的列添加到原始 Dat
假设我有一个列表: a = [3, 5, 1, 1, 3, 2, 4, 1, 6, 4, 8] 和一个子列表: b = [5, 2, 6, 8] 我想通过 pd.qcut(a,2) 获取 bin 并计
有没有一种方法可以构造 Pandas groupby 和 qcut 命令以返回具有嵌套图 block 的一列?具体来说,假设我有 2 组数据,我希望将 qcut 应用于每组,然后将输出返回到一列。这类
我的问题和上一个一样: Binning with zero values in pandas 但是,我仍然想在分位数中包含 0 值。有没有办法做到这一点?换句话说,如果我有 600 个值,其中 50%
有两个 ndarray: import pandas as pd import numpy as np a = np.arange(0,100, 10) b = np.random.random_in
我对 groupby 结果应用 qcut,以下是我的问题的简化版本: a = pd.DataFrame({'A':[1,1,1,1,2,2,2,2], 'B': [
我正在做 Recency-Frequency-Monetary 分析,虽然我有一个在 Python 中工作的模型,但由于生产代码主要是 PHP(Oracle 12c fwiw 或者也可以在 postg
我在名为example 的对象的列中有很多分数。我想将这些分数分成十分位数,并为每一行分配相应的十分位数间隔。我尝试了以下方法: import random import pandas as pd r
更新:从版本 0.20.0 开始,pandas cut/qcut 确实可以处理日期字段。参见 What's New了解更多。 pd.cut and pd.qcut now support dateti
我正在使用 pandas qcut 将一些数据分成 20 个 bin,作为数据准备的一部分,用于训练二进制分类模型,如下所示: data['VAR_BIN'] = pd.qcut(cc_data[va
我有一个包含 499 条记录的埃博拉数据集。我试图根据概率(概率变量)找到每个五分位数中的观察次数。观察次数应分为 0-20%、20-40% 等类别。我认为这样做的代码是, test = pd.qcu
我是一名优秀的程序员,十分优秀!