答案 0 :(得分:0)
我知道的唯一解决方案是在执行groupby
之前重置索引。我在下面做了一个简单的可再现示例,它必须适应您的用例。
它应该有效,但可能有更好的解决方案。我来看看。
# Creating test data
np.random.seed(0)
df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)),
columns=list('ABCD'))
df = df.set_index(['A', 'B'])
# Reset the index,
# group by the first level and count the number of second level
# nunique can also be used to get the number of unique values
df.reset_index(level=1).groupby(level=0)['B'].count()
# A
# 2 1
# 3 1
# 4 1
# 5 3
# 7 2
# 8 2
我认为这是一个更好的解决方案,在索引上使用了很棒的value_counts
方法。
df.reset_index(level=1).index.value_counts()
# 5 3
# 8 2
# 7 2
# 4 1
# 3 1
# 2 1
答案 1 :(得分:0)
census_df = census_df.set_index(['STNAME'])
#this sets all the indices according to STNAME with multiple occurences of each STNAME
census_df.index.value_counts().index[0]
# .index gives all the indices present multiple times
# .value_counts() returns a series with number of occurence of each index sorted from max -> low
#.index[0] gives the STNAME with max occurences = max no. of counties