Python &引用;TypeError:无法将布尔值转换为numpy.ndarray“;按多列分组时
我想将dataframe分为两列,以总结每个商店的月平均销售额 数据(Python &引用;TypeError:无法将布尔值转换为numpy.ndarray“;按多列分组时,python,pandas,dataframe,pandas-groupby,Python,Pandas,Dataframe,Pandas Groupby,我想将dataframe分为两列,以总结每个商店的月平均销售额 数据(factdataframe): 基于store\u id或month的分组效果很好,但当我尝试同时按store\u id和month进行分组时,我得到: groupby_month = fact['quantity'].groupby(fact['store_id', 'month']) ---------------------------------------------------------------------
fact
dataframe):
基于store\u id
或month
的分组效果很好,但当我尝试同时按store\u id
和month
进行分组时,我得到:
groupby_month = fact['quantity'].groupby(fact['store_id', 'month'])
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1 groupby_month=事实['quantity']。groupby(事实['store_id','month'])
2.
3.
D:\Anaconda3\lib\site packages\pandas\core\frame.py in\uuuuu getitem\uuuuuuu(self,key)
2925如果self.columns.nlevels>1:
2926返回自我。\u获取项目\u多级(键)
->2927 indexer=self.columns.get_loc(键)
2928如果是_整数(索引器):
2929索引器=[索引器]
get\U loc中的D:\Anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2655‘回填或最近的查找’)
2656尝试:
->2657返回发动机。获取位置(钥匙)
2658除键错误外:
2659返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine。_get_loc_duplicates()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.\u可能会获得\u bool\u indexer()
TypeError:无法将bool转换为numpy.ndarray
首先检查索引标签和列
fact.index
fact.columns
如果需要将索引转换为列,请使用:
使用:
然后您可以使用:
fact.groupby(['store_id', 'month'])['quantity'].mean()
fact['mean']=fact.groupby(['store_id', 'month'])['quantity'].transform('mean')
print(fact)
store_id sku_id date quantity city city.1 category month \
0 354 31253 2017-08-08 1 Paris Paris Shirt 8
1 354 31253 2017-08-19 1 Paris Paris Shirt 8
2 354 31258 2017-07-30 1 Paris Paris Shirt 7
3 354 277171 2017-09-28 1 Paris Paris Shirt 9
4 174 295953 2017-08-16 1 London London Shirt 8
mean
0 1
1 1
2 1
3 1
4 1
输出:
store_id month
174 8 1
354 7 1
8 1
9 1
Name: quantity, dtype: int64
或更好:
fact.groupby(['store_id', 'month'])['quantity'].mean()
fact['mean']=fact.groupby(['store_id', 'month'])['quantity'].transform('mean')
print(fact)
store_id sku_id date quantity city city.1 category month \
0 354 31253 2017-08-08 1 Paris Paris Shirt 8
1 354 31253 2017-08-19 1 Paris Paris Shirt 8
2 354 31258 2017-07-30 1 Paris Paris Shirt 7
3 354 277171 2017-09-28 1 Paris Paris Shirt 9
4 174 295953 2017-08-16 1 London London Shirt 8
mean
0 1
1 1
2 1
3 1
4 1
首先检查索引标签和列
fact.index
fact.columns
如果需要将索引转换为列,请使用:
使用:
然后您可以使用:
fact.groupby(['store_id', 'month'])['quantity'].mean()
fact['mean']=fact.groupby(['store_id', 'month'])['quantity'].transform('mean')
print(fact)
store_id sku_id date quantity city city.1 category month \
0 354 31253 2017-08-08 1 Paris Paris Shirt 8
1 354 31253 2017-08-19 1 Paris Paris Shirt 8
2 354 31258 2017-07-30 1 Paris Paris Shirt 7
3 354 277171 2017-09-28 1 Paris Paris Shirt 9
4 174 295953 2017-08-16 1 London London Shirt 8
mean
0 1
1 1
2 1
3 1
4 1
输出:
store_id month
174 8 1
354 7 1
8 1
9 1
Name: quantity, dtype: int64
或更好:
fact.groupby(['store_id', 'month'])['quantity'].mean()
fact['mean']=fact.groupby(['store_id', 'month'])['quantity'].transform('mean')
print(fact)
store_id sku_id date quantity city city.1 category month \
0 354 31253 2017-08-08 1 Paris Paris Shirt 8
1 354 31253 2017-08-19 1 Paris Paris Shirt 8
2 354 31258 2017-07-30 1 Paris Paris Shirt 7
3 354 277171 2017-09-28 1 Paris Paris Shirt 9
4 174 295953 2017-08-16 1 London London Shirt 8
mean
0 1
1 1
2 1
3 1
4 1
需要添加“作为_index=True”
例:
“count_in=df.groupby(['time_in','id'],as_index=True)['time_in'].count()”需要添加“as_index=True”
例:
“count\u in=df.groupby(['time\u in','id'],as\u index=True)['time\u in'].count()”摆脱
fact
内部groupby()?您提供的数据帧肯定有一个'store\u id'
列。它不是索引,是吗?去掉fact
中的groupby()
:fact['quantity'].groupby(['store\u id','month'])
?现在我得到了一个KeyError:'store\u id'你确定吗?您提供的数据帧肯定有一个'store\u id'
列。它不是索引,是吗?