Python3, Pandas 5 lines of code to achieve read and write operations on excel

Pandas read and write excel

pandas write excel data

Before using pandas , you need to install the pandas module, the
old method:

pip install pandas

After the installation is complete, you can use it directly,
on the code

# -*- coding:utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-8-13
"""
import pandas as pd
import  os

#DataFrame 数据帧,相当于工作簿中的一个工作表
df = pd.DataFrame({
    
    
    'id':[1,2,3,4],
    'name':['张三','李四','王五','赵刘'],
    'arg': [10,20,30,40],
    'score':[99,88,77,66]
})
#自定义索引,否则pandas会使用默认索引,导致工作表也会存在这些索引
cf = df.set_index('id')
# print(cf)
#设置文件保存路径
data_path = "../py_class/data"
#没有,则创建
if  not os.path.exists(data_path):
    os.mkdir(data_path)
#设置文件名,以xlsx格式
data_name = os.path.join(data_path,'data.xlsx')
#把DataFrame的数据写入excel表
cf.to_excel(data_name)

print("done")

Finally, the execution is complete, and the
id is used for indexing. The result is as follows:
Insert picture description here

Using the pandas default index, the result is like a sauce:
Insert picture description here

Therefore, if you want to use the pandas default index or create the index yourself, see what you like~~

pandas read excel data

Reading excel data is also very simple.
Let’s list various situations first, and then read the data

#设置文件名字,没用os.path方法,因为这不是重点
data_name= '../py_class/data/data.xlsx'

#header=2表示从第三行开始(默认0),跳过前两行,sheet_name表示工作表名称
pepl = pd.read_excel(data_name,header=2,sheet_name="Sheet1")
# pepl = pd.read_excel(data_name,sheet_name="Sheet1")
#打印列名
# print(pepl.columns)

#sort_values 进行排序,by 针对某一行,ascending= False 表示从大到小,inplace = True  直接编辑当前的文档
pepl.sort_values(by = "score", ascending= False, inplace=True)
print(pepl)

#如果读取的excel中无开头标题,可将header=None,手动进行设置
pepl = pd.read_excel(data_name,header=None)
#设置 4个字段,id,name,arg,score
pepl.columns = ['id','name','arg','score']
print(pepl.columns)

#指定id列为索引
pepl = pd.read_excel(data_name,index_col = 'id')
#输出,此时就不会产生索引
print(pepl.head())

#skiprows开头跳过几行,usecols使用哪些列中的数据,dtype设置某一列的类型
pepl = pd.read_excel(data_name,skiprows = 4,usecols = 'E:H',dtype={
    
    "id":str,"gender":str,"brithday":str})

Let’s take a look at the data we’ve written above and how it’s input

# -*- coding:utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-8-13
"""

import pandas as pd

#设置文件名字
data_name= '../py_class/data/data.xlsx'

#读取data_name文件,sheet页是sheet1
peple = pd.read_excel(data_name,sheet_name="Sheet1")
#print(peple.head())

#sort_values 进行排序,by 针对sorce,ascending= False 表示从大到小,inplace = True  就地编辑
peple.sort_values(by = "score", ascending= False, inplace=True)
#
print(peple)

The output is just like sauce:
Insert picture description here

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/107973075