Python实训day09pm【Python处理Excel实际应用】

目录

1、实训分组

2、练习题

2.1、课堂练习1

2.2、课堂练习2


1、实训分组

4人一组,完成实训大作业和实训报告。

每组只提交一份大作业 和 一份实训报告。附上一个说明文档:组长来说明每个人的工作量。

①咱们两个班参与本次实训的同学共计72名同学,分为18个组;
②每组为4名同学;
③每组第1名同学为组长,其余3名同学为组员;
④大家自行结合进行分组,分组名单于今天晚上8点前完成,后续实训报告等事宜由小组形式进行,每组提交一份即可。
大家互相转告,辛苦@所有人。

爬虫、图片、DataFrame等结合应用的大作业,最终成绩:日常考勤+(日常作业)+大作业,周末班主任找到大家核对本周考勤。

2、练习题

2.1、课堂练习1

下午的题目任务:

真实生活应用场景:有两个excel表格,
总录取名单.xlsx,表1:总的学生录取信息(考号、学号、姓名、性别、专业等);
我的名单.xls,表2:我的学生名单(姓名、性别、身份证号、考号、学院等信息),唯独没有学号信息

要求:
1.根据考号的对照,将总名单中的学号,加入到我的名单中。
2.可能会有个别同学没有被录取,总录取名单中找不到该生的考号,将这些人找出来,并将学号设置为0。

左上角 File ---> Settings ---> project ---> +号搜索安装openpyxl。openpyxl

import pandas as pd
import xlrd
import xlwt

# 读取
total = pd.read_excel(r'C:\Users\lwx\Desktop\总名单.xlsx');
sub = pd.read_excel(r'C:\Users\lwx\Desktop\我的学生名单.xls');

# 根据sub中的“准考证号”,到total中与“考生号”比较,找到对应行的“学号”

sub_zkz = list(sub['准考证号']);
print(len(sub_zkz))  # 625
total_ksh = list(total['考生号']);
print(len(total_ksh))  # 8635

xhs = [];
# 逐个查找每个准考证号,在total中的第几行(索引)
for zkz in sub_zkz:
    if int(zkz) in total_ksh:
        index = total_ksh.index(int(zkz));  # 行下标(索引)
        xh = total.loc[index, '学号'];  # 找到学号
        xhs.append(xh);
    else:
        xhs.append(0);

print(xhs)

sub['学号'] = xhs;

# 导出
sub.to_excel(r'C:\Users\lwx\Desktop\处理后.xls', index=False);  # index=False,不要行索引
print("Over")



F:\Python38\python.exe F:/JetBrains/2pythonSpace/pythonCode/实训lwx作业/day09-pm-01.py
625
8635
F:/JetBrains/2pythonSpace/pythonCode/实训lwx作业/day09-pm-01.py:30: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  sub.to_excel(r'C:\Users\lwx\Desktop\处理后.xls', index=False);  # index=False,不要行索引
Over

Process finished with exit code 0

2.2、课堂练习2

将表1中的分析样的行数据,拷贝到表2中对应的“那些”行里:
例如:
dw001 ---> dw001A dw001B
dw002 ---> dw002A dw002B dw002C dw002D

'''
将表1中的分析样的行数据,拷贝到表2中对应的“那些”行里:
例如:
dw001 ---> dw001A dw001B
dw002 ---> dw002A dw002B dw002C dw002D
最终得到充填数据后的表2。
'''
import pandas as pd
import os

df1 = pd.read_excel(r'C:\Users\lwx\Desktop\1.xlsx')
df2 = pd.read_excel(r'C:\Users\lwx\Desktop\2.xlsx')

tls = list(df1.iloc[:, 0])
sub_tls = list(df2.iloc[:, 0])

for i, t in enumerate(sub_tls):
    temp = str.upper(t)[:-1]
    i1 = tls.index(temp)
    sj = list(df1.iloc[i1])
    sj[0] = t
    df2.loc[i] = sj
    print('生成第%d行数据' % (i + 1))
    print(sj)

df2.to_excel(r'C:\Users\lwx\Desktop\result.xls')
print('生成完成,结果存入:%s' % os.path.abspath(r'C:\Users\lwx\Desktop\result.xls'))

吾日三省吾身。——《论语·学而》

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

upward337

谢谢老板~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值