[英]python pandas string column time rolling distinct count
As I want to count the unique number of column A in a moving time window(60 seconds): 由于我想计算移动时间窗口(60秒)中列A的唯一数量:
fn = lambda x: len(np.unique(x))
df = pd.DataFrame({'A':['a', 'b', 'a', 'b', 'e'], 'B': [0, 1, 2, 3, 4]},
index = [pd.Timestamp('20130101 09:01:00'),
pd.Timestamp('20130101 09:01:32'),
pd.Timestamp('20130101 09:02:03'),
pd.Timestamp('20130101 09:02:25'),
pd.Timestamp('20130101 09:03:06')])
df[['A']].rolling('60s').apply(fn)
I expect the result as 我期望结果为
2013-01-01 09:01:00 1
2013-01-01 09:01:32 2
2013-01-01 09:02:03 2
2013-01-01 09:02:25 2
2013-01-01 09:03:06 2
however, the result is: 但是,结果是:
2013-01-01 09:01:00 a
2013-01-01 09:01:32 b
2013-01-01 09:02:03 a
2013-01-01 09:02:25 b
2013-01-01 09:03:06 e
what's the problem? 有什么问题?
You can use column B
instead A
: 您可以使用
B
列而不是A
列:
a = df[['B']].rolling('60s').apply(fn)
print (a)
B
2013-01-01 09:01:00 1.0
2013-01-01 09:01:32 2.0
2013-01-01 09:02:03 2.0
2013-01-01 09:02:25 3.0
2013-01-01 09:03:06 2.0
And if need convert to int
: 如果需要转换为
int
:
a = df[['B']].rolling('60s').apply(fn).astype(int)
print (a)
B
2013-01-01 09:01:00 1
2013-01-01 09:01:32 2
2013-01-01 09:02:03 2
2013-01-01 09:02:25 3
2013-01-01 09:03:06 2
If no column you can create it: 如果没有列,则可以创建它:
a = df.assign(B=np.arange(len(df.index)))[['B']].rolling('60s').apply(fn).astype(int)
print (a)
B
2013-01-01 09:01:00 1
2013-01-01 09:01:32 2
2013-01-01 09:02:03 2
2013-01-01 09:02:25 3
2013-01-01 09:03:06 2
df['B'] = np.arange(len(df.index))
a = df[['B']].rolling('60s').apply(fn).astype(int)
print (a)
B
2013-01-01 09:01:00 1
2013-01-01 09:01:32 2
2013-01-01 09:02:03 2
2013-01-01 09:02:25 3
2013-01-01 09:03:06 2
EDIT1: EDIT1:
df['B'] = np.arange(len(df.index))
a = df.groupby('A')[['B']].rolling('60s').apply(fn).astype(int)
print (a)
B
A
a 2013-01-01 09:01:00 1
2013-01-01 09:02:03 1
b 2013-01-01 09:01:32 1
2013-01-01 09:02:25 2
e 2013-01-01 09:03:06 1
Simply try this way : 只需尝试这种方式:
In [40]: import pandas as pd
In [41]: fn = lambda x: len(np.unique(x))
...: df = pd.DataFrame({'A':['a', 'b', 'c', 'd', 'e'], 'B': [0, 1, 2, 3, 4]},
...: index = [pd.Timestamp('20130101 09:01:00'),
...: pd.Timestamp('20130101 09:01:32'),
...: pd.Timestamp('20130101 09:02:03'),
...: pd.Timestamp('20130101 09:02:25'),
...: pd.Timestamp('20130101 09:03:06')])
In [42]: df[['B']] = df[['B']].rolling('60s').apply(fn).astype(int)
In [43]: df[['']] = df[['B']]
In [44]: df[['']]
Out[44]:
2013-01-01 09:01:00 1
2013-01-01 09:01:32 2
2013-01-01 09:02:03 2
2013-01-01 09:02:25 3
2013-01-01 09:03:06 2
In [45]:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.