前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据处理的 3 个小技巧,都很实用

数据处理的 3 个小技巧,都很实用

作者头像
double
发布2020-05-08 11:43:47
4310
发布2020-05-08 11:43:47
举报
文章被收录于专栏:算法channel算法channel算法channel

个人原创,一字一字码的

数据处理无所不在,掌握常用技巧,事半功倍。

此系列使用 Pandas 开展数据处理分析,总结其中常用、好用的数据分析技巧。

我使用的 Pandas 版本如下,顺便也导入 Pandas 库。

>>> import pandas as pd
>>> pd.__version__
'0.25.1'

今天使用的数据集名称:IMDB-Movie-Data,取自 Kaggle,百度网盘下载链接如下:

链接: https://pan.baidu.com/s/15u7Hf2y5dSFwek2vA1-zjg 提取码: bvfx

在开始前先确保解释器和数据集在同一目录下:

>>> 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()

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-04-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员郭震zhenguo 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 Pandas 移除某列
  • 2 统计标题单词数
  • 3 Genre 频次统计
相关产品与服务
Prometheus 监控服务
Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档