In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use pandas to achieve a filter control in Python, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Flexibility of PivotTable
When we get a piece of data, we often need to constantly change the conditions to observe the data, such as the sales of a fruit:
A row of data indicates that one day (date) the price (AveragePrice) and sales volume (Total Volume) of a variety (type) of this fruit in a certain region (region).
Hope to see the top 10 regions of total sales in different years, as well as the sales information of each variety.
Because requirements need to be summarized, using PivotTable in Excel is the best way:
Without saying much about the process, it is worth noting that we can select different years at the top, and the data table below can immediately display the regional data of the total sales of top 10 in that year.
Can this effect be achieved quickly and easily with Python?
Small component
We will use the ipywidgets library, which is most suitable for Jupyter Notebook. Assuming you have installed Jupyter Notebook, open your cmd and execute the following command:
1pip install ipywidgets & & jupyter nbextension enable-- py widgetsnbextension
Until you see the following message:
1Enabling notebook extension jupyter-js-widgets/extension...
2-Validating: ok
Just restart your Jupyter Notebook now.
Note:
If you don't know how to install Anaconda or use Jupyter Notebook, you can check out my previous article
The widgets library is used to complete various widgets. It can generate web content, not only for use in Jupyter Notebook, but this article only describes the use on Jupyter Notebook.
Embellish your pandas processing
The libraries that need to be imported in this article are these:
First use pandas to get the results of the PivotTable, which is very simple:
Line 3pr 4: to highlight what can be changed, two variables are defined here
By modifying two variables, we can get the corresponding result data.
But this is far from enough. If you want to see the results of different years or different Top N, you need to modify the code.
Let's process this process a little bit and define a function:
It's better now, but you still need to modify the code to change the conditions.
Simply add a decorator:
Line 1:@wg.interact is a decorator that is typed into our custom function
Where each named parameter is a parameter on a custom function
X_df=wg.fixed (df), which means that the x_df parameter uses the variable df, and this parameter does not need to be changed. So there is no option for this variable on the interface.
Year= [2015, 2016, 2017, 2018], there are four choices for the year, and a drop-down box of year can be seen on the interface for users to click on.
Topn=range (1meme 11), there are 10 options for topn, and drop-down boxes can also be seen in the interface.
Now, we just need to simply select the condition value from the drop-down box, and the result below will be refreshed immediately, just like the PivotTable in Excel.
However, as we all know, you can also create a perspective based on a PivotTable in Excel, and here we can also produce dynamic charts:
Line 13514: using the display method, you can output the content
The above is how to use pandas to implement a filter control in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.