清理数据 python

In this post, we will be using the Pandas library with Python to demonstrate how to clean the real-world data so that it is ready-to-be-used for any processing task. Frankly, most of the data obtained from real-world sensors contain a lot of garbage and null values. Oftentimes, data are in a format that is not supported by data analysis or machine learning algorithms. Hence data cleansing is almost always the first step in most data analytics/ML/AI jobs.

在本文中,我们将使用Pandas库和Python来演示如何清除现实世界的数据,以便随时将其用于任何处理任务。 坦白说,从现实世界的传感器获得的大多数数据都包含大量垃圾和空值。 通常,数据采用数据分析或机器学习算法不支持的格式。 因此,在大多数数据分析/ ML / AI作业中,数据清理几乎始终是第一步。

Pandas is an immensely powerful library for data manipulation. Earlier, I was a huge Matlab fan and thought that there is no match for Matlab when it comes to data analysis. But ever since I have moved to Pandas (Python actually), I hate going back to Matlab again.

Pandas是一个功能强大的数据处理库。 早些时候,我是Matlab的忠实拥护者,并认为在数据分析方面Matlab无法与之匹敌。 但是自从我搬到Pandas(实际上是Python)以来,我就讨厌再次回到Matlab。

Anyways, keep Matlab aside now, and let’s begin with Pandas.

无论如何,现在就把Matlab放在一边,让我们从熊猫开始吧。

The data that I will be using is the real-world call data record (CDR) that was made public by Telecom Italia as part of a Big Data competition in 2014. You can find and download the complete dataset here (I should warn that this is a very huge data set. Each file is over 300 MB, and there are 62 files in total — one file per day). I am sharing one file only that I will be using in this blog via my google drive. Click here to get that file.

我将使用的数据是真实电话数据记录(CDR),该数据是由Italia Telecom在2014年大数据竞赛中公开发布的。您可以在此处找到并下载完整的数据集 (我应该警告这是一个非常大的数据集。每个文件超过300 MB,总共有62个文件-每天一个文件)。 我仅共享一个文件,该文件将通过Google驱动器在此博客中使用。 单击此处获取该文件。

The dataset captures the calls, SMS, and internet usage of Telecom Italia’s users in the city of Milan, Italy for two whole months. Each day is recorded as a single file. However, for this blog, I will only be using the data for a single day (i.e. single file) from this CDR.

该数据集捕获了意大利米兰市两个月的意大利电信用户的呼叫,短信和互联网使用情况。 每天都记录为一个文件。 但是,对于此博客,我将仅使用该CDR中一天的数据(即单个文件)。

Let’s first start with importing all the necessary packages

让我们首先从导入所有必要的包开始

import pandas as pdfrom pandas import read_csv

After importing all the necessary packages, let’s do the real stuff. Pandas provide a function read_csv(…) (which we have imported earlier) to read different kinds of data files. In our files, the data is stored in a tab-delimited format. Hence we will use <delimiter = ‘\t’> argument to specify during the reading process to break whenever a tab (\t) exist in the file.

导入所有必需的程序包后,让我们开始实际工作。 熊猫提供了一个read_csv(…)函数(我们之前已经导入了它)来读取不同类型的数据文件。 在我们的文件中,数据以制表符分隔的格式存储。 因此,我们将使用<delimiter ='\ t'>参数指定在读取过程中只要文件中存在制表符(\ t)时就中断。

I always prefer not to mess with the actual variable that stores data. Hence, we will clone data in another dataframe and call it df. The command df.head() will display the first few members of the dataset.

我始终不希望弄乱存储数据的实际变量。 因此,我们将在另一个数据帧中克隆数据,并将其称为df 。 命令df.head()将显示数据集的前几个成员。

dataset = pd.read_csv('D:/Research/dataset/sms-call-internet-mi-2013-11-1.txt', delimiter='\t')df = datasetdf.head()

Observe that the dataset is in a raw format (See Fig. 1 below). Even the names of the columns are not mentioned.

观察到数据集是原始格式的(请参见下面的图1)。 甚至没有提到列名。

Fig. 1: Raw data from Telecom Italia
图1:来自意大利电信的原始数据

First of all, we will give appropriate names to all the columns using df.columns. In this particular case, the dataset provider (i.e. Telecom Italia) has given all the information about the columns. Hence we will use this information to appropriately name each column.

首先,我们将使用df.columns为所有列指定适当的名称。 在这种特殊情况下,数据集提供者(即意大利电信)已经给出了有关列的所有信息。 因此,我们将使用此信息来适当地命名每一列。

df.columns = ['Grid ID', 'Time Stamp','Caller ID','SMS in',              'SMS out','Call in','Call out','Internet']

数据清理 (Data Cleansing)

Data cleansing is very crucial and almost always-needed (as mentioned earlier) step when working with real-world data as captured data may have a lot of discrepancies, missing values, etc.

处理真实数据时,数据清理非常关键,几乎总是需要采取的步骤(如前所述),因为捕获的数据可能存在很多差异,缺少值等。

For example, observe that in Figure 1 above that there are several NaN values within the raw dataset. These values indicate the data acquiring sensors could not get any values for whatever reasons. We will replace all NaN values with Zero (0). For this purpose, pandas provide a simple function fillna(…). We will use this function to replace NaN with Zeros (0). Also, note that using inplace = True is equivalent to stating df = df.fillna(0). This is another strong feature in pandas that allow a cleaner and shorter version of code.

例如,观察上面的图1,原始数据集中有多个NaN值。 这些值表明数据采集传感器无论出于何种原因都无法获得任何值。 我们将所有NaN值替换为零(0)。 为此,熊猫提供了一个简单的功能fillna(...) 。 我们将使用此功能将NaN替换为零(0)。 另外,请注意,使用inplace = True等效于声明df = df.fillna(0) 。 这是熊猫的另一个强大功能,可以使代码更简洁,更短。

The time unit for each record entry is given in milliseconds. Hence we will also change the time unit into minutes. Finally, we will display the formatted data in Figure 2 below.

每个记录条目的时间单位以毫秒为单位。 因此,我们还将时间单位更改为分钟。 最后,我们将在下面的图2中显示格式化的数据。

# Fill all the NaN values with 0df.fillna(0,inplace = True)#The time is in milli-seconds. We will change it to minutes.df['Time Stamp'] = df['Time Stamp']/(1000*60)df.head()
CDR after cleansing
清洁后的CDR

Observe that all NaN values are replaced by 0. The timestamp has also been changed into minutes.

观察所有NaN值都被替换为0。时间戳记也已更改为分钟。

Finally, we will display the Internet activity to observe what kind of data we have (You can play around with other activities such as Call in, Call out, etc as well). You can try an insert other activities as well.

最后,我们将显示Internet活动以观察我们拥有什么样的数据(您也可以参与其他活动,如“呼入”,“呼出”等)。 您也可以尝试插入其他活动。

%matplotlib inlineimport matplotlibimport matplotlib.pyplot as pltplt.plot(df['Internet'])plt.grid()plt.xlabel('Time (in 10-minute interval)', fontsize =12)plt.ylabel('Activity', fontsize = 12)

We will stop here this week. Next week, we will use this cleaner version of data to predict cellular traffic using Deep Neural Network (Recurrent Neural Net). Till then…

我们本周将在这里停止。 下周,我们将使用更干净的数据版本,通过深度神经网络(递归神经网络)预测蜂窝网络流量。 直到那时…

The complete code is present here.

完整的代码在这里

翻译自: https://medium.com/analytics-vidhya/data-cleansing-using-pandas-in-python-33204242d3b2

清理数据 python


http://www.taodudu.cc/news/show-5199730.html

相关文章:

  • 日志系统优化选型之采集端
  • 辣鸡采集,采集世界上所有辣鸡数据 欢迎大家来采集
  • 电子表格控件SpreadJS V14.0 Update2全新来袭——增强表格编辑器
  • VBA打开公式编辑器
  • 急,大数据实验室出了问题怎么解决
  • 贴息贷款政策下,高校建立大数据实验室新思路
  • 深圳市大数据研究院(医疗大数据实验室)招聘博士、博士后、硕士以及科研助理...
  • 阿里云大数据实验室:MaxCompute使用体验
  • 大数据实验室:零基础学习大数据该看哪些书?
  • 中科院西光所成立时空大数据实验室,打造大数据运算核心基础平台
  • 搭建独立大数据实验室的设想
  • 【调剂】山东交通学院山东省大数据发展创新实验室2023接收调剂
  • 上海成立司法行政大数据实验室
  • 大数据实验室作业总结
  • 大数据实验室
  • “大数据“实验室建设解决方案
  • 通信原理包络是什么意思_直缝钢管的矫直原理
  • 圆管带式输送机毕业设计(说明书+CAD图纸+任务书+答辩+翻译……)
  • 带式输送机设计【毕业论文+CAD图纸+开题报告+中期报告+外文翻译】
  • 如何进行立体仓库项目招标
  • 穿梭车特点介绍
  • 【智能存储】自动化立体仓库中的自动化系统解析
  • 物流智能机器人—结合WMS系统进行通讯的海格里斯(HEGERLS)单轨环形RGV穿梭车
  • 码垛机器人集成配置案例说明
  • 工业输送带裁切技术要点总结
  • 短视频开发,Android 应用接入新浪微博分享
  • 微博广告投放如何定向推广?在微博推广广告有效果吗?
  • 如何微博推广呢?
  • 【随笔】新浪微博分析
  • 无师自通第四天

清理数据 python_在python中使用熊猫清理数据相关推荐

  1. python中ret是什么意思_数据结构图在python中的应用

    原标题:数据结构图在python中的应用 程序世界里,有很多的数据结构,比如:堆.栈.链表等等,今天要讲的就是图数据结构啦. 相信大家都使用过或者听说过图数据库吧,我们就来看看最简单的图数据结构算法. ...

  2. Python语言编程学习:numpy中的array格式数据切片与pandas中的dataframe格式数据切片、相互转换

    Python语言编程学习:numpy中的array格式数据切片与pandas中的dataframe格式数据切片.相互转换 目录 numpy中的array格式数据切片与pandas中的dataframe ...

  3. Python中通过索引名称提取数据loc()函数Python中通过行和列下标提取数据iloc()函数

    [小白从小学Python.C.Java] [Python全国计算机等级考试] [Python数据分析考试必会题] ● 标题与摘要 Python中通过索引名称提取数据 loc()函数 Python中通过 ...

  4. python 获取金融数据_class类在python中如何获取金融数据

    class类在python中如何获取金融数据 发布时间:2020-12-11 11:12:06 来源:亿速云 阅读:101 作者:小新 这篇文章主要介绍了class类在python中如何获取金融数据, ...

  5. Brats2020数据集的读取—>python中对.nii格式数据读取

    Brats2020数据集的读取->python中对.nii格式数据读取 首先要安装了torchio,安装指令: pip install torchio torchio:一个 Python 库,用 ...

  6. 在Python中FITS格式文件数据的读取 (转载)

    在Python中FITS格式文件数据的读取 (转载) 前言 \space\space\space\space     FITS(Flexible Image Transport System)格式文件 ...

  7. Python中pandas库实现数据缺失值判断isnull()函数

    [小白从小学Python.C.Java] [Python全国计算机等级考试] [Python数据分析考试必会题] ● 标题与摘要 Python中pandas库实现数据缺失值判断 isnull()函数 ...

  8. python随机生成一组数据_使用Python random模块生成随机数据实例

    在本节中,我们将学习如何使用random模块(random)在Python中生成随机数和数据.该模块为各种分布(包括整数,浮点数(实数))实现了伪随机数生成器. 本文的目标: 以下是我们将在本文中介绍 ...

  9. 动量策略 python_在Python中使用动量通道进行交易

    动量策略 python Most traders use Bollinger Bands. However, price is not normally distributed. That's why ...

最新文章

  1. Docker入门六部曲——Stack
  2. SURF算法与源码分析、上
  3. textarea中的换行符
  4. cas 注销不关闭浏览器异常_上海公司经营异常注销麻烦吗
  5. 无权无向和加权网络的聚类系数
  6. 27、很酷的C语言技巧
  7. 信用卡还不起会有什么严重后果?
  8. python 批量下载网页图片_Python实现多线程批量下载图片
  9. 了解有关JDK9紧凑弦乐的信息(视频回顾Charlie Hunt)
  10. vue 二进制文件的下载(解决乱码和解压报错)
  11. php中购物车功能,php如何实现购物车功能
  12. Codeforces 964B(贪心)
  13. Linux中 vim 编辑器的使用【详细】
  14. 第29期:蓝桥杯练习-2022/2/12
  15. beanutils初步
  16. HTML5分级标题,HTML5与CSS3基础教程:创建分级标题_html/css_WEB-ITnose
  17. 2018江苏高考数学第16题计算量大不大你自己看着办
  18. 美国伊利诺伊大学香槟分校计算机专业,伊利诺伊大学香槟分校
  19. 第四周-C语言 圆柱体表面积计算
  20. 【经典算法实现 3】冒泡排序算法(单向冒泡,双向冒泡)

热门文章

  1. ATL Server 与 ASP.NET
  2. python日出日落时间实现和详解
  3. 杂_小技巧_将网页上的内容通过亚马逊邮箱传到kindle中
  4. Docker部署MongoDB
  5. java多线程下LongAdder、CountDownLatch、CyclicBarrier、Phaser 的用法
  6. 【Lin-CMS内容管理系统框架 v0.3.6】内置用户管理/权限管理/日志系统等常见功能
  7. 怎样使用CAD在nVisual中创建楼层场景
  8. 创客——新工业革命读书笔记
  9. 功率器件参数测试仪系统@半导体器件电学特性测试
  10. JQuery添加或移除CSS样式 页签