需求:对一个excel中的两列数据进行排列组合,并将数据追加写入到表格中。
背景:完全不懂代码的小白,所以,最后将代码打包生成可执行文件,方便操作。
下面直接上代码。
import pandas as pd
import os
def find_excel(path):
for (root, dirs, files) in os.walk(path):
for file in files:
if str(file).endswith("xlsx") or str(file).endswith("xls"):
return file
def get_res(file):
data = pd.read_excel(file)
col1 = data.iloc[:, 1].dropna().tolist()
col2 = data.iloc[:, 2].dropna().tolist()
l = []
for col1_i in col1:
for col2_i in col2:
l.append(str(col1_i)+str(col2_i))
merge = pd.DataFrame(data=[col1, col2, l], index=["col1", "col2", "col3"]).T
return merge
def write_excel(file, res):
with pd.ExcelWriter(file, engine='openpyxl', mode="a", if_sheet_exists="replace") as xlsx:
res.to_excel(xlsx, sheet_name="res", index=None)
if __name__ == '__main__':
file = find_excel(r"./")
res = get_res(file)
write_excel(file, res)
生成可执行文件
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller #安装pyinstaller
pyinstaller -F -w -n merge py2excel2pailie.py #下一行也可以,自行运行,查看区别
pyinstaller -F -w -xxx.py
需求是第2和第3列变为第4列

最后执行效果为显示为下图:

最后,手动获取需求列,方法有点笨,还望指正!
博客针对完全不懂代码的小白,提出对Excel两列数据进行排列组合并追加写入表格的需求,还要求将代码打包成可执行文件。随后给出代码,最后手动获取需求列,希望得到指正。

317

被折叠的 条评论
为什么被折叠?



