Python数据处理的三个实用技巧分享
作者:Python学习与数据挖掘 时间:2023-07-01 23:37:13
我使用的 Pandas 版本如下,顺便也导入 Pandas 库。
>>> import pandas as pd
>>> pd.__version__
'0.25.1'
在开始前先确保解释器和数据集在同一目录下:
>>> import os
>>> os.chdir('D://source/dataset') # 这是我的数据集所在目录
>>> os.listdir() # 确认此目录已经存在 IMDB-Movie-Data 数据集
['drinksbycountry.csv', 'IMDB-Movie-Data.csv', 'movietweetings', 'titanic_eda_data.csv', 'titanic_train_data.csv']
准备工作就位后,正式开始数据处理技巧之旅。
1 Pandas 移除某列
导入数据
>>> df = pd.read_csv("IMDB-Movie-Data.csv")
>>> df.head(1) # 导入并显示第一行
Rank Title Genre ... Votes Revenue (Millions) Metascore
0 1 Guardians of the Galaxy Action,Adventure,Sci-Fi ... 757074 333.13 76.0
[1 rows x 12 columns]
使用 pop 方法移除指定列:
>>> meta = df.pop("Title").to_frame() # 移除 Title 列
确认是否已被移除:
>>> df.head(1) # df 变为 11列
Rank Genre ... Revenue (Millions) Metascore
0 1 Action,Adventure,Sci-Fi ... 333.13 76.0
[1 rows x 11 columns]
2 统计标题单词数
pop 后得到 meta,显示 meta 前 3 行:
>>> meta.head(3)
Title
0 Guardians of the Galaxy
1 Prometheus
2 Split
标题是由单词组成,中间用空格分隔。
# .str.count(" ") + 1 得到单词个数
>>> meta["words_count"] = meta["Title"].str.count(" ") + 1
>>> meta.head(3) # words_count 列代表单词个数
Title words_count
0 Guardians of the Galaxy 4
1 Prometheus 1
2 Split 1
3 Genre 频次统计
下面统计电影 Genre 的频次,
>>> vc = df["Genre"].value_counts()
下面显示电影 Genre 的 Top5 ,最高频为出现 50 次的 Action,Adventure,Sci-Fi 类,次之为 48 次的 Drama 类:
>>> vc.head()
Action,Adventure,Sci-Fi 50
Drama 48
Comedy,Drama,Romance 35
Comedy 32
Drama,Romance 31
Name: Genre, dtype: int64
展示 Top5 的饼状图:
>>> import matplotlib.pyplot as plt
>>> vc[:5].plot(kind='pie')
<matplotlib.axes._subplots.AxesSubplot object at 0x000001D65B114948>
>>> plt.show()
来源:https://blog.csdn.net/weixin_38037405/article/details/123869850
标签:Python,数据处理,技巧
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django 简单实现分页与搜索功能的示例代码
2023-12-26 01:40:16
![](https://img.aspxhome.com/file/2023/0/124400_0s.png)
实操MySQL+PostgreSQL批量插入更新insertOrUpdate
2024-01-17 09:06:19
三招解决SQL Server数据库权限冲突
2009-03-16 16:58:00
python求最大值,不使用内置函数的实现方法
2021-02-06 09:13:12
MySQL InnoDB 锁的相关总结
2024-01-18 16:14:55
![](https://img.aspxhome.com/file/2023/3/82263_0s.png)
Python叠加两幅栅格图像的实现方法
2023-07-25 17:14:06
![](https://img.aspxhome.com/file/2023/5/61485_0s.jpg)
Linux 安装二进制MySQL 及 破解MySQL密码的方法
2024-01-24 16:12:38
Python嵌套循环的使用
2022-11-12 17:19:23
![](https://img.aspxhome.com/file/2023/5/101095_0s.png)
vue使用v-if v-show页面闪烁,div闪现的解决方法
2024-04-28 09:31:49
解决pycharm回车之后不能换行或不能缩进的问题
2023-06-11 10:45:40
![](https://img.aspxhome.com/file/2023/2/75962_0s.jpg)
python文件读取失败怎么处理
2023-05-06 17:14:46
web前端页面性能优化
2009-08-15 12:31:00
SQL注入攻击成为新威胁将挑战操作系统安全
2009-03-16 15:13:00
python中map的基本用法示例
2023-09-24 15:56:26
![](https://img.aspxhome.com/file/2023/1/124141_0s.png)
Go语法糖之‘...’ 的使用实例详解
2024-04-26 17:16:43
详解如何修改jupyter notebook的默认目录和默认浏览器
2022-07-01 14:34:54
![](https://img.aspxhome.com/file/2023/7/69017_0s.png)
与MSSQL对比学习MYSQL的心得(四)--BLOB数据类型
2024-01-20 12:32:53
Python Matplotlib简易教程(小白教程)
2023-12-29 05:31:57
![](https://img.aspxhome.com/file/2023/9/124289_0s.png)
一篇文章搞懂:词法作用域、动态作用域、回调函数及闭包
2022-10-01 00:21:05
![](https://img.aspxhome.com/file/2023/4/64994_0s.png)
python 实现检验33品种数据是否是正态分布
2022-09-25 19:57:55