votes up 3

Series.count level is only valid with a MultiIndex

github stars 30911
Exception Class:

Raise code

                "Using the level keyword in DataFrame and Series aggregations is "
                "deprecated and will be removed in a future version. Use groupby "
                "instead. ser.count(level=1) should use ser.groupby(level=1).count().",
            if not isinstance(self.index, MultiIndex):
                raise ValueError("Series.count level is only valid with a MultiIndex")

        index = self.index
        assert isinstance(index, MultiIndex)  # for mypy

        if isinstance(level, str):
            level = index._get_level_number(level)
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 1 votes down

Error code:

import pandas as pd
import numpy as np

index = [1,2,3,4,6,7,8]
s = pd.Series(np.random.randn(8),index)

Pandas.Series.count returns number of non-NA/null observations in the Series.

If the index of Series is not instance of MultiIndex , it will not possible to count the number of non-Na/null observations.

Fix code:

import pandas as pd
import numpy as np

iterables = [["bar""baz""foo""qux"], ["one""two"]]
index = pd.MultiIndex.from_product(iterables,names=["first""second"])
# We created MultiIndex and initial it as a index of Series
s = pd.Series(np.random.randn(8),index)

Sep 30, 2021 anonim answer
anonim 13.0k

Add a possible fix

Please authorize to post fix