Introduction to Data Analysis with PyCharts and Pandas
In this article, we will explore how to display daily histograms of total amount by type using PyCharts and Pandas. We will start by importing the necessary libraries, loading the data, and cleaning it up.
Importing Libraries
To begin, we need to import the necessary libraries. The first library we’ll be using is Pandas, which provides high-performance data structures and operations for Python. The next library we’ll be using is PyCharts, which allows us to easily create various types of charts and graphs.
import pandas as pd
import pycharts
Loading the Data
Next, we need to load our data into a Pandas DataFrame. In this case, we’re assuming that our data is stored in an Excel file called orders.xlsx and that it has a sheet named “Sheet1”.
df = pd.read_excel('./orders.xlsx', sheet_name='Sheet1')
Cleaning Up the Data
Before we can start analyzing our data, we need to clean it up. In this case, we’ll be removing the dollar signs from the price column and converting it to an integer.
df['price'] = df['price'].replace('$','', regex=True).astype(int)
We’ll also create a new column called new that’s the product of the num and price columns.
df['new'] = df['price'].mul(df['num'])
Grouping by Month
Now that our data is cleaned up, we can start grouping it by month. We’ll use the groupby function to group our DataFrame by the date column, which will give us a new DataFrame with the average value of each group.
July_df = df[df['date'].dt.to_period('M')=='2020-07'].copy()
Plotting the Data
Finally, we can start plotting our data. We’ll use the groupby function to group our DataFrame by month and then plot a pie chart of the total amount for each type.
(df.groupby([pd.Grouper(key='date',freq='M'),'name'])['total'].sum()
.reset_index(level='date')
.groupby('date')
.plot.pie(subplots=True, autopct='%.2f%%'))
This will give us a pie chart showing the total amount for each type by month.
Additional Formatting
We can also add some additional formatting to our plot. We’ll use the subplots function to create two subplots side-by-side and then loop through each group, plotting a pie chart of the total amount for that group.
groups = (df.groupby([df.date.dt.strftime('%b-%Y'),'name'])['total'].sum()
.reset_index(level='date')
.groupby('date'))
fig, axes = plt.subplots(1,2, figsize=(10,5))
for ax, (month, data) in zip(axes, groups):
data['total'].plot.pie(autopct='%.2f%%', ax=ax)
ax.set_title(f'data in {month}')
This will give us two separate pie charts showing the total amount for each type by month.
Conclusion
In this article, we’ve explored how to display daily histograms of total amount by type using PyCharts and Pandas. We started by loading our data into a Pandas DataFrame, cleaning it up, and grouping it by month. Then, we used the groupby function to plot pie charts of the total amount for each type by month. Finally, we added some additional formatting to our plot to make it more visually appealing.
Output
The output will be two separate pie charts showing the total amount for each type by month. The first pie chart will show a pie chart of the total amount for each type in July, and the second pie chart will show a pie chart of the total amount for each type in August.

Last modified on 2024-09-19