I think there is a misunderstanding on what the .count()
method returns.
Try to follow this example:
Create a sample data frame
df = pd.DataFrame({
'A':[0,1,0,1, 1],
'B':[100,200,300, 400, 500],
'C': [1,2,3,4,5]
})
This is what the count()
method will return after groupby
# similarly to your example I am grouping by A and counting
df.groupby([df.A]).count()

As you can see, the count()
method returns a dataframe itself, having the count of each other column values for the column where the grouped column has the same value.
After that, you can query for a specific column form the return of count()
like this
df.groupby([df.A]).count()['C']
But the second case in your example, which in my example would correspond to df.groupby([df.A]).count()[df.C]
Will throw an error!

In fact, you would query a dataframe (in this case df.groupby([df.A]).count()
) via a pandas Series
but as you know you need a string or a column from df.columns
.
You can check yourself that df.C
and 'C'
are two very different variable types.
print(type(df.C))
print(type('C'))
# <class 'pandas.core.series.Series'>
# <class 'str'>
If for some reason your code still works with the equivalent of df.C
there might be some contingency like the only value of the df.C
is a string with the same name of a column.. or something unintentional like that.