Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Multi-column Frequency Statistics of data Table in the New version of pandas

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how the new version of pandas to achieve data table multi-column frequency statistics, I hope you will gain something after reading this article, let's discuss it together!

Column frequency statistics

In previous versions of pandas (before 1.1), there were single-column frequency statistics.

Let's take the passenger data of the Titanic as an example:

Image-20200806092628285

Want to quickly view the number of records for each gender:

Image-20200806092732878

The absolute value is shown above. Can you show the percentage?

Image-20200806092901143

It can be converted into proportion through the parameter normalize

However, the above are for single-column statistics, most of the time we want to multi-column combination of frequency statistics.

Multi-column Frequency Statistics of data Table

Now that the method value_counts of the same name has been appended to DataFrame in pandas version 1.1, let's take a look at how to use it.

Check gender and survival statistics:

Image-20200806093401214

The first parameter is subset, and you can pass the list of the column name to be counted.

Similarly, the output can be fine-tuned through the parameters:

Image-20200806093527710

It can be seen that the male survival rate is very low.

It may be confusing for some beginners, is it troublesome for the old version to get this result?

In fact, it is very easy for previous versions to achieve the same effect:

Image-20200806094104421

No output in reverse order of frequency?

Simply "connect" the sort operation at the end, and the result is the same:

Image-20200806094230946

In fact, there is another way to solve the problem. If you are interested in looking at the source code, please.

The statistical proportion is not much of a thing:

Image-20200806094306039

Then you must use the new version of the method. The new method has done so many things for you in one sentence!

But!

Next, let's look at the advantages of "making your own decisions".

Sectional statistics

In the previous explanation of single-column frequency statistics (Series.value_counts), I actually omitted a useful parameter that can only be used for numeric columns.

Want to check the frequency of the age group (divided into 3 paragraphs):

Image-20200806095018867

The bins parameter is specified in 3 segments.

Usually we want to sort by segment:

Image-20200806095136997

The parameter sort controls whether to reverse the order by frequency. If set to False, sort by index

Do you think the new version of DataFrame.value_counts also has this parameter?

Unfortunately, there is no such parameter, and it should be taken into account that the values of combined columns cannot be segmented.

But for custom functions, of course you can do whatever you want:

Image-20200806100144613

Because the DataFrame.groupby itself can support mixed types of key. So when key is set, it can be a column name (a string), a column value, or a mixture of them

After reading this article, I believe you have a certain understanding of "how to achieve data table multi-column frequency statistics in the new version of pandas". If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report