Data analysis, split Excel case and code implementation

  • Before you start processing data, you need to clarify your ideas
  • The next step is to sort out the processing flow
  • Understand the ideas and processes, and finally the code implementation process

1.DataFrame data structure

2. Read CSV

3. DataFrame remove duplication

4.DataFrame filter data

5.Matplotlib visualizes data and generates pictures for Pandas

6. Save DataFrame data and pictures to Excel

Import related libraries pandas, xlwings

import pandas as pd
import xlwings as xw
import matplotlib.pyplot as plt
import matplotlib
app=xw.App(visible=False,add_book=True)
df=pd.read_csv('gpd_csv.csv')
df_country=df['Country Name'].drop_duplicates()
count=1
for name in df_country:
	df_new=df[df['Country Name']==name]
	wb=xw.Book()
	sht=wb.sheets[0]
	sht.range('A1').value=df_new
	fig=plt.figure()
	plt.plot(df_new['Year'],df['Value'],label='GDP')
	plt.tile('name'+'的GDP的增长趋势')
	plt.grid()
	plt.legend()
	matplot.rcParams['font.sans-serif']=['SimHei']
	plt.show()
	sht.pictures.add(fig,left=sht.range('h2').left,top=sht.range('h2').top)
	wb.save('./coun_name/+str(name)+'.xlsx')
	sht.clear()
	wb.close()
	count+=1
	if count >5:
	break

The results after running the code are as follows:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

At the same time, files in the computer disk have been successfully written in batches:

Insert picture description here

Open any Excel file:

Insert picture description here

  • Summary: The above is only 5 pieces of data written to verify the feasibility of the code execution,
    then the whole process is divided and written to disk in batches, and at the same time, the picture is drawn and inserted into the corresponding Excel. The
    whole process looks super cool.

Guess you like

Origin blog.csdn.net/weixin_42961082/article/details/114798386