数据科学家花了大量的时间清洗数据集,并将这些数据转换为他们可以处理的格式。事实上,很多数据科学家声称开始获取和清洗数据的工作量要占整个工作的80%。
因此,如果你正巧也在这个领域中,或者计划进入这个领域,那么处理这些杂乱不规则数据是非常重要的,这些杂乱数据包括一些缺失值,不连续格式,错误记录,或者是没有意义的异常值。
在这个教程中,我们将利用Python的 Pandas 和 Numpy 包来进行数据清洗。
主要内容如下:
- 删除 DataFrame 中的不必要 columns
- 改变 DataFrame 的 index
- 使用 .str() 方法来清洗 columns
- 使用 DataFrame.applymap() 函数按元素的清洗整个数据集
- 重命名 columns 为一组更易识别的标签
- 滤除 CSV文件中不必要的 rows
下面是要用到的数据集:
- BL-Flickr-Images-Book.csv - 一份来自英国图书馆包含关于书籍信息的CSV文档
- university_towns.txt - 一份包含美国各大洲大学城名称的text文档
- olympics.csv - 一份总结了各国家参加夏季与冬季奥林匹克运动会情况的CSV文档
你可以从 Real Python 的 GitHub repository 下载数据集来进行下面的例子。
注意:建议使用Jupter Notebooks来学习下面的知识。
学习之前假设你已经有了对Pandas和Numpy库的基本认识,包括Pandas的工作基础 Series 和 DataFrame 对象,应用到这些对象上的常用方法,以及熟悉了NumPy的 NaN 值。如果你想了解大数据的学习路线,想学习大数据知识以及需要免费的学习资料可以加群:784789432.欢迎你的加入。每天下午三点开直播分享基础知识,晚上20:00都会开直播给大家分享大数据项目实战。
让我们导入这些模块开始我们的学习。
- >>> import pandas as pd
- >>> import numpy as np
删除DataFrame的列
经常的,你会发现数据集中不是所有的字段类型都是有用的。例如,你可能有一个关于学生信息的数据集,包含姓名,分数,标准,父母姓名,住址等具体信息,但是你只想分析学生的分数。
这个情况下,住址或者父母姓名信息对你来说就不是很重要。这些没有用的信息会占用不必要的空间,并会使运行时间减慢。
Pandas提供了一个非常便捷的方法 drop() 函数来移除一个DataFrame中不想要的行或列。让我们看一个简单的例子如何从DataFrame中移除列。
首先,我们引入 BL-Flickr-Images-Book.csv 文件,并创建一个此文件的DataFrame。在下面这个例子中,我们设置了一个 pd.read_csv 的相对路径,意味着所有的数据集都在 Datasets 文件夹下的当前工作目录中:
- >>> df = pd.read_csv('Datasets/BL-Flickr-Images-Book.csv')
- >>> df.head()
- Identifier Edition Statement Place of Publication \
- 0 206 NaN London
- 1 216 NaN London; Virtue & Yorston
- 2 218 NaN London
- 3 472 NaN London
- 4 480 A new edition, revised, etc. London
- Date of Publication Publisher \
- 0 1879 [1878] S. Tinsley & Co.
- 1 1868 Virtue & Co.
- 2 1869 Bradbury, Evans & Co.
- 3 1851 James Darling
- 4 1857 Wertheim & Macintosh
- Title Author \
- 0 Walter Forbes. [A novel.] By A. A A. A.
- 1 All for Greed. [A novel. The dedication signed... A., A. A.
- 2 Love the Avenger. By the author of “All for Gr... A., A. A.
- 3 Welsh Sketches, chiefly ecclesiastical, to the... A., E. S.
- 4 [The World in which I live, and my place in it... A., E. S.
- Contributors Corporate Author \
- 0 FORBES, Walter. NaN
- 1 BLAZE DE BURY, Marie Pauline Rose - Baroness NaN
- 2 BLAZE DE BURY, Marie Pauline Rose - Baroness NaN
- 3 Appleyard, Ernest Silvanus. NaN
- 4 BROOME, John Henry. NaN
- Corporate Contributors Former owner Engraver Issuance type \
- 0 NaN NaN NaN monographic
- 1 NaN NaN NaN monographic
- 2 NaN NaN NaN monographic
- 3 NaN NaN NaN monographic
- 4 NaN NaN NaN monographic
- Flickr URL \
- 0 http://www.flickr.com/photos/britishlibrary/ta...
- 1 http://www.flickr.com/photos/britishlibrary/ta...
- 2 http://www.flickr.com/photos/britishlibrary/ta...
- 3 http://www.flickr.com/photos/britishlibrary/ta...
- 4 http://www.flickr.com/photos/britishlibrary/ta...
- Shelfmarks
- 0 British Library HMNTS 12641.b.30.
- 1 British Library HMNTS 12626.cc.2.
- 2 British Library HMNTS 12625.dd.1.
- 3 British Library HMNTS 10369.bbb.15.
- 4 British Library HMNTS 9007.d.28.
我们使用了 head() 方法得到了前五个行信息,这些列提供了对图书馆有帮助的辅助信息,但是并不能很好的描述这些书籍: Edition Statement , Corporate Author , Corporate Contributors , Former owner , Engraver , Issuance type and Shelfmarks 。
因此,我们可以用下面的方法移除这些列:
- >>> to_drop = ['Edition Statement',
- ... 'Corporate Author',
- ... 'Corporate Contributors',
- ... 'Former owner',
- ... 'Engraver',
- ... 'Contributors',
- ... 'Issuance type',
- ... 'Shelfmarks']
- >>> df.drop(to_drop, inplace=True, axis=1)
在上面,我们定义了一个包含我们不要的列的名称列表。接着,我们在对象上调用 drop() 函数,其中 inplace 参数是 True , axis 参数是 1 。这告诉了Pandas我们想要直接在我们的对象上发生改变,并且它应该可以寻找对象中被移除列的信息。
我们再次看一下DataFrame,我们会看到不要想的信息已经被移除了。
- >>> df.head()
- Id