按组划分的日期范围内的有效熊猫滚动计数-Python 2.7.13 Windows-熊猫0.20.2
我正在努力在数据帧的分组子集内执行滚动时间感知计数,这将在特定日期为每个ActivityType和ContactId动态创建新列 我相信我在调用groupby和rolling方法时使用了正确的语法,但我仍然收到一个TypeError:插入列的索引与框架索引不兼容——我不知道为什么 非常感谢您的时间和帮助 预期产出:按组划分的日期范围内的有效熊猫滚动计数-Python 2.7.13 Windows-熊猫0.20.2,python,python-2.7,pandas,pandas-groupby,Python,Python 2.7,Pandas,Pandas Groupby,我正在努力在数据帧的分组子集内执行滚动时间感知计数,这将在特定日期为每个ActivityType和ContactId动态创建新列 我相信我在调用groupby和rolling方法时使用了正确的语法,但我仍然收到一个TypeError:插入列的索引与框架索引不兼容——我不知道为什么 非常感谢您的时间和帮助 预期产出: ContactId ActivityType Date Call_90D Meeting_90D Conference_90D Webinar_90D David
ContactId ActivityType Date Call_90D Meeting_90D Conference_90D Webinar_90D
David Call 01/01/2015 1 0 0 0
David Meeting 01/17/2015 1 1 0 0
David Call 02/12/2015 2 1 0 0
Ryan Conference 01/01/2015 0 0 1 0
Ryan Meeting 01/17/2015 0 1 1 0
Ryan Webinar 02/12/2015 0 1 1 1
Louis Call 03/29/2015 1 0 0 0
Louis Call 03/30/2015 2 0 0 0
Louis Webinar 09/01/2015 0 0 0 1
Bryan Call 01/12/2015 1 0 0 0
Bryan Call 01/17/2015 2 0 0 0
Bryan Call 02/01/2015 3 0 0 0
代码
错误:
line 20, in <module>
df[new_col_name] = df.groupby(by='ContactId', as_index=False, group_keys=False).rolling(window='90D', on='Date')[a].sum()
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2331, in __setitem__
self._set_item(key, value)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2397, in _set_item
value = self._sanitize_column(key, value)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2547, in _sanitize_column
value = reindexer(value)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2542, in reindexer
raise TypeError('incompatible index of inserted column '
TypeError: incompatible index of inserted column with frame index
第20行,在
df[new\u col\u name]=df.groupby(by='ContactId',as\u index=False,group\u keys=False)。滚动(window='90D',on='Date')[a]。求和()
文件“C:\Python27\lib\site packages\pandas\core\frame.py”,第2331行,在\uuu setitem中__
自我设置项目(键、值)
文件“C:\Python27\lib\site packages\pandas\core\frame.py”,第2397行,在集合项中
value=self.\u清理\u列(键,值)
文件“C:\Python27\lib\site packages\pandas\core\frame.py”,第2547行,在_sanitize_列中
值=重新索引器(值)
reindexer中的文件“C:\Python27\lib\site packages\pandas\core\frame.py”,第2542行
raise TypeError('插入列的索引不兼容'
TypeError:插入列的索引与框架索引不兼容
能否打印df[df['ActivityType']==a]的长度
和df
在第一个循环中:df=12,df_subset=7;这肯定是个问题。所以我按类型计数的方法是关闭的,它影响了将数据从分组的_df映射回原始df的能力。感谢您花时间查看@Wen。您认为有可能完成我试图做的事情吗?@Wen-仅供参考,我从代码中删除了df[df['ActivityType']==a]
,以测试滚动groupby是否可以工作并且仍然接收相同的值ErrorLet我们将df.Date=pd.to_datetime(df.Date)
Dude,更改此行(添加值
)df[new\u col\u name]=df.groupby(by='ContactId',as\u index=False,group\u keys=False).rolling(window='90D',on='Date')[a].sum().values
:-)
line 20, in <module>
df[new_col_name] = df.groupby(by='ContactId', as_index=False, group_keys=False).rolling(window='90D', on='Date')[a].sum()
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2331, in __setitem__
self._set_item(key, value)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2397, in _set_item
value = self._sanitize_column(key, value)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2547, in _sanitize_column
value = reindexer(value)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2542, in reindexer
raise TypeError('incompatible index of inserted column '
TypeError: incompatible index of inserted column with frame index