Python 计算唯一值会引发维度错误

Python 计算唯一值会引发维度错误,python,pandas,pandas-groupby,unique,Python,Pandas,Pandas Groupby,Unique,我将输入作为一个数据帧new\u res,行数>6m。我的目标是获得所有唯一行的计数 start_hex_id_res8 start_hex_id_res9 end_hex_id_res9 end_hex_id_res9 date is_weekday is_holiday starthour 0 882a100d23fffff 892a100d23bffff 892a100d237ffff 892a100d237ffff 2020-07-01 True False

我将输入作为一个数据帧
new\u res
,行数>6m。我的目标是获得所有唯一行的计数

start_hex_id_res8   start_hex_id_res9   end_hex_id_res9 end_hex_id_res9 date    is_weekday  is_holiday  starthour
0   882a100d23fffff 892a100d23bffff 892a100d237ffff 892a100d237ffff 2020-07-01  True    False   0
1   882a100d23fffff 892a100d23bffff 892a100d237ffff 892a100d237ffff 2020-07-01  True    False   0
2   882a1072c7fffff 892a1072c6bffff 892a1072187ffff 892a1072187ffff 2020-07-01  True    False   0
3   882a1072c7fffff 892a1072c6bffff 892a1072187ffff 892a1072187ffff 2020-07-01  True    False   0
4   882a100d09fffff 892a100d097ffff 892a100d09bffff 892a100d09bffff 2020-07-01  True    False   0

start_hex_id_res8    object
start_hex_id_res9    object
end_hex_id_res9      object
end_hex_id_res9      object
date                 object
is_weekday             bool
is_holiday             bool
starthour             int64
我试过了

agg = new_res.groupby(['start_hex_id_res8', 'start_hex_id_res9', 'end_hex_id_res9', 'end_hex_id_res9', 'date','is_weekday', 'is_holiday', 'starthour']).size().groupby(level=0).size()
但这带来了一个错误:

ValueError: Grouper for 'end_hex_id_res9' not 1-dimensional
我应该如何解释这一点,正确的方法是什么,以创建一个新的数据帧,它是
new_res
的浓缩版本?输出将只是一个具有相同列名的数据帧,但具有所有唯一行的计数(在末尾添加一个
count
列)

g=df.apply(lambda x:x.astype(str))#Make entire dataframe a str
g.groupby(list(g.columns)).ngroup().nunique()#Groupbycolumns, find special groups and see how many are unique
让我们试试看

g=df.apply(lambda x:x.astype(str))#Make entire dataframe a str
g.groupby(list(g.columns)).ngroup().nunique()#Groupbycolumns, find special groups and see how many are unique

为什么你们有两列同名“end\U hex\U id\U res9”?为什么你们有两列同名“end\U hex\U id\U res9”?请给我一票。我对你的问题投了赞成票。谢谢,请给我一票。我对你的问题投了赞成票。谢谢