In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Python how to make sales data visual Kanban". In daily operation, I believe many people have doubts about how to make sales data visual Kanban in Python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "Python how to make sales data visual Kanban". Next, please follow the editor to study!
In the data age, the importance of sales data analysis is needless to say.
Only with an accurate analysis of the sales data can we find out the reasons for the changes (growth or decline) of the data.
Then it will be possible to solve problems and find new growth points.
Today, I will introduce to you a method of making a big screen of sales data with Python.
Mainly use Python Streamlit library, Plotly library, Pandas library to build.
Among them, Pandas processes data, Plotly makes visual charts, and Streamlit builds visual pages.
For the above three libraries, the Streamlit library may be relatively strange, I will briefly introduce it.
Streamlit is a completely free open source application framework that helps you quickly create a cool Web page without knowing complex front-end technologies such as HTML,CSS.
1. data
The data used is fictional data, a supermarket 2021 sales order data, a total of 1000 order data.
There are three cities, Beijing, Shanghai and Hangzhou. There are two types of customers, member and ordinary. The customer sex is male and female.
The rest also contains information such as order number, product type, unit price, quantity, total price, date, time, payment method, cost, gross margin, total income, score and so on.
The read_excel method of the general Pandas reads data.
Skip the first three rows and select columns B to R, 1000 rows of data.
Def get_data_from_excel (): df = pd.read_excel (io= "supermarkt_sales.xlsx", engine= "openpyxl", sheet_name= "Sales", skiprows=3, usecols= "barr", nrows=1000,) # add hourly column data df ["hours"] = pd.to_datetime (df ["time"] Format= "% H:%M:%S") .dt.hour return dfdf = get_data_from_excel () print (df)
The data was read successfully, and the result is as follows.
Now you can write the page.
two。 Page title and icon
We all know that when a browser opens a web page, it will have a title and icon.
So we need to set the name, icon, layout and so on of this page first.
This is also the first Streamlit command to use to build a page using Streamlit, and can only be set once.
# set web page information st.set_page_config (page_title= "sales data big screen", page_icon= ": bar_chart:", layout= "wide")
The page_icon parameter can use emoji code to display icons.
A complete collection of proper emoji codes!
3. Sidebar and multiple checkboxes
St.sidebar (sidebar), each element passed to st.sidebar is pinned to the left, allowing users to focus on the content on the home page.
Multiselect is an interactive widget through which data can be filtered.
# sidebar st.sidebar.header ("Please filter here:") city = st.sidebar.multiselect ("Select City:", options=df ["City"]. Unique (), default=df ["City"]. Unique ()) customer_type = st.sidebar.multiselect ("Select customer Type:", options=df ["customer Type"]. Unique (), default=df ["customer Type"]. Unique () ) gender = st.sidebar.multiselect ("choose gender:", options=df ["gender"] .unique (), default=df ["gender"] .unique () df_selection = df.query ("City = = @ city & customer type = = @ customer_type & gender = @ gender")
Combined with Pandas's query query, the data can be filtered.
It is successfully built through the above code, as shown on the left side of the figure below.
Click on the upper right corner of the sidebar to close the symbol, and the sidebar can be hidden.
The web page will display the main page.
4. Main page information
Then write the main page information, including the home page title, total sales, average score, average sales information.
Like the icons on the web page, it is implemented through emoji code.
# main page st.title (": bar_chart: big screen of sales data") st.markdown ("# #") # Core indicators, Total sales, average rating, Star rating, average sales data total_sales = int (df_selection ["Total Price"]. Sum ()) average_rating = round (df_selection ["rating"]. Mean (), 1) star_rating = ": star:" * int (round (average_rating) 0) average_sale_by_transaction = round (df_selection ["Total Price"] .mean (), 2) # 3 column layout left_column, middle_column, right_column = st.columns (3) # add related information with left_column: st.subheader ("Total sales:") st.subheader (f "RMB {total_sales: } ") with middle_column: st.subheader (" average score: ") st.subheader (f" {average_rating} {star_rating} ") with right_column: st.subheader (" average sales: ") st.subheader (f" RMB {average_sale_by_transaction} ") # separator st.markdown ("-- ")
Complete the processing of the core index data, and display it in layout.
5. Main page chart
It contains two charts, one is the hourly sales, and the other is the total sales of all kinds of goods. Complete the drawing of the chart through Plotly Express.
Plotly Express is a new advanced Python visualization library and an advanced encapsulation of Plotly.py that provides a simple syntax for complex diagrams.
Inspired by Seaborn and ggplot2, it is specially designed to have a concise, consistent and easy-to-learn API. With a single import, you can create a rich interactive drawing in a function call.
# sales of various types of commodities (bar chart) sales_by_product_line = (df_selection.groupby (by= ["Commodity Type"]). Sum () [["by="] .sort _ values ("by=")) fig_product_sales = px.bar (sales_by_product_line, x = "total price", y=sales_by_product_line.index, orientation= "h", title= "total sales of each commodity" Color_discrete_sequence= ["# 0083B8"] * len (sales_by_product_line), template= "plotly_white",) fig_product_sales.update_layout (plot_bgcolor= "rgba)" Xaxis= (dict (showgrid=False)) # hourly sales (bar chart) sales_by_hour = df_selection.groupby (by= ["hours"]). Sum () [["sales_by_hour.index"]] print (sales_by_hour.index) fig_hourly_sales = px.bar (sales_by_hour, x=sales_by_hour.index, y = "Total Price", title= "Total sales per hour" Color_discrete_sequence= ["# 0083B8"] * len (sales_by_hour), template= "plotly_white",) fig_hourly_sales.update_layout (xaxis=dict (tickmode= "linear"), plot_bgcolor= "rgba", yaxis= (dict (showgrid=False)),) left_column, right_column = st.columns (2) left_column.plotly_chart (fig_hourly_sales, use_container_width=True) right_column.plotly_chart (fig_product_sales Use_container_width=True)
Add data, set chart configuration, and page layout.
The results are as follows.
6. Hide the assembly
When we build an interface through Streamlit, there are red lines, menus, and "Make with Streamlit" at the end by default.
For the sake of beauty, you can hide them all here.
# hide streamlit default format information hide_st_style = "" # MainMenu {visibility: hidden;} footer {visibility: hidden;} header {visibility: hidden;} "" st.markdown (hide_st_style, unsafe_allow_html=True)
Such an interactive sales data Kanban, completed to build!
# install the dependent library pip install-I https://pypi.tuna.tsinghua.edu.cn/simple plotly==4.14.3pip install-I https://pypi.tuna.tsinghua.edu.cn/simple pandas==1.1.0pip install-I https://pypi.tuna.tsinghua.edu.cn/simple streamlit==0.86.0pip install-I https://pypi.tuna.tsinghua.edu.cn/simple openpyxl==3.0.6# to run streamlit run app.py
Install related dependencies and run the program at the command line terminal.
At this point, the study on "how to make sales data visual Kanban by Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.