Python:用了这个库,就可以跟 Excel 说再见了
今天分享一个个比 Excel 更好用的 Python 工具,看完后,估计你要跟 Excel 说拜拜了。它就是 Mito
Mito
Mito 是 Python 中的电子表格库。简单易用,如果你能编辑 Excel 文件,你就能编写代码,这是因为,我们在表格中执行的每个操作,Mito 将自动生成对应的 Python 代码。可以跟重复枯燥的操作说再见了。
官方文档[1]
安装 Mito
安装前确保 Python 的版本在 3.6 及以上。
pip install mitoinstaller
然后执行:
python -m mitoinstaller install
等待命令执行完成。
mitoinstaller 安装程序会为经典的 Jupyter Notebook 和 JupyterLab 安装 Mito。它将自动启动 JupyterLab,你也手动启动 Jupyter Notebook 来使用 Mitosheet。
Mito 读取文件
Excel 对行数有限制。如果打开包含数百万行的文件,该文件将打开,但在 Excel 中您不会看到超过 1,048,576 行。
相比之下,Python 可以处理数百万行。唯一的限制是您的 PC 的计算能力。让我们看看如何使用 Mito 读取文件。
在读取 CSV 文件之前,首先,我们需要创建一个 Mito 电子表格。为此,我们运行下面的代码。
import mitosheet
mitosheet.sheet()
运行之后,就可以读取 CSV 文件了,这里将使用一个包含学校成绩的数据集[2],然后如下所示进行导入。
http://mpvideo.qpic.cn/0bc32eaaqaaaluapbpgnvbrfbuodbdiqacaa.f10002.mp4?dis_k=233e121a1cc12dc8bb9f88ba34e1fc29&dis_t=1666702211&vid=wxv_2484259507410436097&format_id=10002&support_redirect=0&mmversion=false
导入之后,将自动生成以下代码:
import pandas as pd
StudentsPerformance_csv = pd.read_csv(r'StudentsPerformance.csv')
Mito 的自动化
用 Excel 的话,你只能完成基本操作的自动化,而 Mito 没有限制。
使用 Mito,你可以做更多的事情,比如通过电子邮件发送报告,使用 WhatsApp 发送文件,使用 Google 表格作为基本数据库等。
让我们用 Mito 记录一些动作,就好像我们在使用 Excel 一样。
创建/重命名列
http://mpvideo.qpic.cn/0bc3liaaaaaalaap5condzrfawwdabnaaaaa.f10002.mp4?dis_k=b5f6735d85ea7b212061ee823c25f18a&dis_t=1666702211&vid=wxv_2484282955885051906&format_id=10002&support_redirect=0&mmversion=false
会自动生成如下代码:
# Added column new-column-uca5 to StudentsPerformance_csv
StudentsPerformance_csv.insert(8, 'new-column-uca5', 0)
# Renamed new-column-uca5 to average in StudentsPerformance_csv
StudentsPerformance_csv.rename(columns={'new-column-uca5': 'average'}, inplace=True)
求和
http://mpvideo.qpic.cn/0bc3ciaakaaaoeapwm6n7nrfaewdaujaabia.f10002.mp4?dis_k=73755d55f1f505a322bc25e8f5d7f4e7&dis_t=1666702211&vid=wxv_2484284584633958401&format_id=10002&support_redirect=0&mmversion=false
会自动生成如下代码:
# Set new-column-uca5 in StudentsPerformance_csv to =(math score+reading score+writing score)/3
StudentsPerformance_csv['average'] = (StudentsPerformance_csv['math score']+StudentsPerformance_csv['reading score']+StudentsPerformance_csv['writing score'])/3
创建数据透视表
http://mpvideo.qpic.cn/0bc344aaqaaayaapoewnvbrfbz6dbdtqacaa.f10002.mp4?dis_k=1cc6d99195576af5e809d3e8b315d6fc&dis_t=1666702211&vid=wxv_2484287874545352705&format_id=10002&support_redirect=0&mmversion=false
会自动生成如下代码:
# Imported StudentsPerformance.csv
import pandas as pd
StudentsPerformance_csv = pd.read_csv(r'StudentsPerformance.csv')
# Pivoted StudentsPerformance_csv into df2
unused_columns = StudentsPerformance_csv.columns.difference(set(['race/ethnicity']).union(set([])).union(set({'math score', 'reading score'})))
tmp_df = StudentsPerformance_csv.drop(unused_columns, axis=1)
pivot_table = tmp_df.pivot_table(
index=['race/ethnicity'],
values=['math score', 'reading score'],
aggfunc={'math score': ['mean'], 'reading score': ['mean']}
)
pivot_table.columns = [flatten_column_header(col) for col in pivot_table.columns.values]
df2 = pivot_table.reset_index()
创建一个柱状图
使用 Mito 可以轻松创建饼图和条形图等基本可视化。我们只需要点击“图表”并选择图表类型。
让我们为之前创建的数据透视表创建一个条形图,在 X 轴上显示“种族/民族”,在 Y 轴上显示“数学分数平均值”:
http://mpvideo.qpic.cn/0bc35uaaqaaagaapn5gnvbrfb3odbdwqacaa.f10002.mp4?dis_k=8ebc3521bd00cf92ee6d44f772ce92ee&dis_t=1666702211&vid=wxv_2484286640782442496&format_id=10002&support_redirect=0&mmversion=false
不错吧,a、b、c 和 d 中生成的代码行相当于 Excel 宏。每次我们运行代码时,Mito 都会执行所有记录的动作。
参考资料
[1]
官方文档: https://docs.trymito.io/getting-started/installing-mito
[2]
数据集: https://drive.google.com/file/d/1V9Hw_N73zsc56j8J9R8DrluSmi5yU3eg/view
相关文章
- python不报错但计算不出结果_excel表格不能用公式怎么办
- Python面试基础知识_python自学需要哪些基础知识
- Python获取时间戳_python爬虫时间戳
- python十进制转换_Python 进制转换
- excel宏 java,Microsoft Excel宏运行Java程序
- Python lambda函数的学习
- python监控网站更新_Python 通过网站search功能监控网站内容更新[通俗易懂]
- Python: yield用法的解析
- python读取pkl_Python读取文件的一段内容
- python的特点和优势_Java与Python异同
- python获取字典中键对应的键值
- Python 遍历 dict
- Python中优先级_低优先级队列不止5把
- pycharm如何调试python程序_Pycharm断点调试Python程序的步骤方法
- 使用Python批量筛选上千个Excel文件中的某一行数据并另存为新Excel文件(下篇)
- python web框架之FastApi
- python实现将excel数据添加到mongodb详解编程语言
- Visual Studio运行Python程序(超级详细)
- 将Excel表格数据导入MySQL数据库(excel到mysql)
- mssql数据快速转换称Excel格式(mssql转excel)
- Linux系统下安装Python模块指南(linux安装python模块)
- python实现给字典添加条目的方法