Python processes JSON data and generates a bar chart, pythonjson
I. JSON data preparation
First, prepare a piece of JSON data. There are a total of 3560 pieces of data. The structure of each piece of content is as follows:
This example mainly analyzes the distribution of time zones in the Field tz (timezone.
2. Convert JSON data into a Python dictionary
The Code is as follows:
3. Count the distribution of tz values and generate statistical results in the form of "Time Zone: Total number"
To achieve this goal, you must firstRecordsConvertDataFrame,DataFrameYesPandasThe most important data structure, which can represent the data in the form of a table, and then usevalue_counts()
Method summary:
4. Generate a bar chart based on statistical results
Before generating a bar chart, you can add a value (expressed by Missing) to the Missing time zone in the result to complete the data ), the missing value in each time zone also needs to be added with an Unknown value (indicated by Unknown ):
Then useplot()
The method can generate a bar chart:
Here is a complete example of processing JSON data to generate statistical results and bar charts. However, you can further process the statistical results to get more detailed results.
Each piece of data contains an agent value, that is, the browser's USER_AGENT information. This information can be used to know the operating system used, therefore, the statistical results generated in the previous step can be distinguished by operating system.
Agent value:
5. differentiate bar charts by operating system (Windows/non-Windows)
Not all data has the field. First, the data without the agent value is filtered out. Then, the data is grouped based on the time zone and operating system list, and the grouping result is counted:
Finally, select the data in the 10 time zones with the most occurrences to generate a bar chart:
In this way, the bar chart statistics results are differentiated by different operating systems:
The above is the full content of Python processing JSON data and generating bar charts. I hope this article will be helpful for you to learn Python and JSON.