Python automated sorting Excel spreadsheet

I believe many of my friends deal with their daily work will use Excel spreadsheet file kinds, what is more likely to spend a lot of time to do the tedious and time-consuming finishing work table. A friend recently asked whether consolidation program to reduce the workload of the table, Today we come to realize Python automated sorting the table by way of example.

First, we have such a data table source.csv:

Python automated sorting Excel spreadsheet

We have to do is to extract data from the above table, to generate a spreadsheet meets the following requirements:

  1. The following list group.xls groups to organize the data in the table:

Python automated sorting Excel spreadsheet

  1. The final item to show the data:

Python automated sorting Excel spreadsheet

  1. Wherein "K Data / 60" as the data in the table "data K" after / 60 reserved 2 decimals

We look at how to deal with the above requirements manual Excel: read each piece of data to be read in source.csv data table, put group.xls match of group members, the data items required for final screening, and then the specific "data K "for arithmetic processing.

Python then how will it work? Here we have to use powerful pandas library.

pandas NumPy is a tool, the tool to solve data analysis tasks created based on. Pandas included a large library and some standard data model provides the tools needed to efficiently operate large data sets. pandas provides a number of functions and methods enable us to quickly and easily handle the data. You will soon find that it is one of the important factors that make Python become a powerful and efficient data analysis environment. pandas Baidu Encyclopedia

First import pandas library, reading csv and xls table of contents by related functions:


在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
import pandas as pd
# 读取 group.xls 分组信息
group = pd.read_excel("group.xls",header=None)
group.columns=["分组","角色"]
print(group)
# 读取 source.csv 获取所有成员数据
source = pd.read_csv("source.csv")
print(source)

We may first data item source.csv in screening, data entry needs to have a "role", "number", "Data B", "Data C", "data D" and "data K":


# 通过 iloc[:,[列坐标]] 来定位需要的各列数据
filter_merge = source.iloc[:,[0,2,4,5,6,13]]
print(filter_merge)

Next it is to match a packet based on the role of character data, noting group.xls and source.csv shared "role" one, two tables we can pass this fusing to form a matching filling effect.


combine = pd.merge(group,filter_merge,on="角色")

Next we insert operation after the second column "data K / 60":


combine.insert(1,"数据K/60",round(filter_merge["数据K"]/60,2))

In the end, we will generate a new data format written xlsx table:


combine.to_excel(excel_writer="result.xlsx",index=False)

The final automatically generated the following table:

Python automated sorting Excel spreadsheet


The above is an Excel spreadsheet sort of simple implementation of Python code

Guess you like

Origin blog.51cto.com/14510224/2438667