question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Combined value_counts()

See original GitHub issue

Rather often, especially when exploring the data or creating reports, one is interested both in pd.Series.value_counts() and `pd.Series.value_counts(normalize=True). I prepared a branch which implements a wrapper which takes care of this (link)

  • Does it make sense?
  • Is this implementation meeting pandas’ standards?
  • Should testing of this simple wrapper be extended?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:17 (16 by maintainers)

github_iconTop GitHub Comments

5reactions
droratacommented, Sep 7, 2017

I guess it is not really worthy. For future reference, I will leave the function I wrote here:

def value_counts_comb(self, sort=True, ascending=False,
                          bins=None, dropna=True):
        """
        A wrapper of value_counts which returns both the counts and the
        normalized view.

        The resulting object will be in descending order so that the
        first element is the most frequently-occurring element.
        Excludes NA values by default.

        Parameters
        ----------
        normalize : boolean, default False
            If True then the object returned will contain the relative
            frequencies of the unique values.
        sort : boolean, default True
            Sort by values
        ascending : boolean, default False
            Sort in ascending order
        bins : integer, optional
            Rather than count values, group them into half-open bins,
            a convenience for pd.cut, only works with numeric data
        dropna : boolean, default True
            Don't include counts of NaN.

        Returns
        -------
        counts : DataFrame
        """
        from pandas.core.algorithms import value_counts
        from pandas.core.reshape.concat import concat
        res_norm = value_counts(self, sort=sort, ascending=ascending,
                      normalize=True, bins=bins, dropna=dropna)
        res_regu = value_counts(self, sort=sort, ascending=ascending,
                      normalize=False, bins=bins, dropna=dropna)
        result = concat([res_norm, res_regu], axis=1, keys=['Ratio', 'Count'])
        return result
2reactions
TomAugspurgercommented, Jul 7, 2017

I’m not aware of any pandas-utils-like libraries, perhaps you could start one?

And to be clear, others in the community may disagree with me, and prefer that it is included in pandas. Let’s wait and hear feedback from other maintainers and users.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to merge pandas value_counts() to dataframe or use it to ...
value_counts() to find the number of occurrences of particular brands. I want to merge those value counts with the respective brands in the...
Read more >
how to combine the output of two pandas df.value_counts ...
I have two df.value_counts() that I'd like to combine into one: Current: y_train.value_counts() 5 545 6 510 7 159 4 42 8 15...
Read more >
9 Pandas value_counts() tricks to improve your data analysis
Pandas value_counts() function returns a Series containing counts of unique values. By default, the resulting Series is in descending order ...
Read more >
8 Python Pandas Value_counts() tricks that make your work ...
The value_counts() function is used to get a Series containing counts of unique values ... This can be done by combining value_counts() with ......
Read more >
pandas.Series.value_counts — pandas 1.5.2 documentation
Return a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found