Skip to Content
Python 用于地理空间数据分析
book

Python 用于地理空间数据分析

by Bonny P. McClain
May 2025
Intermediate to advanced
282 pages
4h 2m
Chinese
O'Reilly Media, Inc.
Content preview from Python 用于地理空间数据分析

第 8 章 数据清理 数据清理

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

处理数据时的一个普遍问题是了解数据的完整性。 数据工程取决于清理、处理和可视化数据的能力。现在,您已经熟悉了基于笔记本的代码编辑器的基本功能和数据集成,无论是本地的 Jupyter Notebook 还是云端的 Google Colab,现在是时候学习如何清理数据了。 数据经常不完整(缺失)、格式不一致或不准确,这些问题通常被称为混乱数据。数据清理就是解决这些问题并为分析做好数据准备的过程。

在本章中,我们将探索一些公开可用的数据集,使用一些可以加载到 Colab 笔记本中的软件包来查找和清理混乱的数据集。 我们将使用 NYPD_Complaint_Data_Historic 数据集,该数据集来自纽约市的开放数据门户--NYC Open Data,于 2021 年 7 月 7 日更新。我过滤了 2020 年的数据,使其更便于查看和操作。您可以根据自己的数据问题过滤数据,并将其导出为 CSV 文件。本章将向你展示如何管理、删除、更新和合并数据,并使用一些有用的 Python 软件包进行处理。数据分析的准确性取决于数据集或数据库的质量,本章将提供评估和解决常见不一致问题的工具。

检查缺失数据

如果您曾经 参加过数据竞赛(如Kaggle 上的竞赛),您可能会注意到,数据集的设计是为了帮助您将注意力集中在特定任务上,如构建可视化。通常情况下,数据已经为你清理过了。现实生活要复杂一些,数据缺失是一个长期存在的问题。让我们来看看如何让你的数据世界更整洁一些。

对于电子表格和表格数据格式,评估数据的形状是一项直接的任务。只需稍微滚动一下,就能轻松发现缺少数据的行和(或)列。当您查看数据集的缺失数据模式时,实际上是在评估数据集的无效性,或是否存在无效数据(缺失值)。地理空间分析和一般分析一样,经常涉及多个表格,因此学习如何识别这些表格数据集之间的数据模式非常重要。

上传到 Colab

本例中我使用的是Google Colab,但您也可以使用自己喜欢的环境。将NYPD Complaint Data HistoricCSV 文件上传到笔记本,然后单击文件名旁边的点。这将为您提供包含在笔记本中的路径(图 8-1)。

Uploading a file to Colab
图 8-1. 将文件上传到 Colab

Missingno是一个 Python 库,用于检测 数据集中的缺失值,并可视化这些缺失值在数据帧中的分布情况,从而更容易发现其中的规律。1Missingno 底下运行着 NumPy、pandas、SciPy、matplotlib 和 seaborn,让底层代码片段更加熟悉。安装 Missingno:

pip install missingno

根据数据集的大小,您可以调整样本大小。在这里,我将样本设置为1000 。导入 Python 库 pandas 可以让你顺便开始一点数据清理和准备工作:

import pandas as pd
NYPD = pd.read_csv("/content/NYPD_Complaint_Data_Historic.csv")
import missingno as msno %matplotlib inline msno.matrix(NYPD.sample( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

实用 Linux 系统管理

实用 Linux 系统管理

Kenneth Hess
ppk on JavaScript

ppk on JavaScript

Peter-Paul Koch
ASP.NET 2.0 Illustrated

ASP.NET 2.0 Illustrated

Alex Homer, Dave Sussman

Publisher Resources

ISBN: 9798341659025