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



我将输入作为熊猫数据帧new_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

我应该如何解释这一点,在panda中创建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

最新更新