美文网首页
2019-10-14

2019-10-14

作者: 数据小黑升值记 | 来源:发表于2019-10-14 20:33 被阅读0次

创建数据 - 我们从创建自己的数据开始。 这避免了阅读这个教程的用户需要去下载任何文件来重现结果。我们将会把这些数据导出到一个文本文件中这样你就可以试着从这个文件中去读取数据。
获取数据 - 我们将学习如何从文本文件中读取数据。 这些数据包含了1880年出生的婴儿数以及他们使用的名字。
准备数据 - 这里我们将简单看一下数据并确保数据是干净的,就是说我们将看一下文件中的数据并寻找一些可能异常的数据。 这可能包括了数据缺失(missing data),数据不一致(inconsistant),或者在正常范围之外(out of place)。 如果有这样的数据,我们将决定如何处置这些数据。
分析数据 - 我们将简单地找出一个给定年份中最热门的名字。
表现数据 - 通过表格和图形,向用户清晰地展示在一个给定的年份中最热门的名字。

除了数据展现的一小部分,pandas 库在数据分析的全过程中将被使用。 matplotlib 只在数据展现部分使用到。 课程的第一步则导入所需要的库。

# 导入所有需要的库

# 导入一个库中制定函数的一般做法: 
##from (library) import (specific library function)
from pandas import DataFrame, read_csv

# 导入一个库的一般做法: 
##import (library) as (give the library a nickname/alias)
import matplotlib.pyplot as plt
import pandas as pd #导入pandas的常规做法
import sys #导入sys库只是为了确认一下Python的版本
import matplotlib #这样导入matplotlib只是为了显示一下其版本号

# 初始化matplotlib,用inline方式显示图形
%matplotlib inline
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
print('Matplotlib version ' + matplotlib.__version__)
Python version 3.6.1 | packaged by conda-forge | (default, Mar 23 2017, 21:57:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Pandas version 0.19.2
Matplotlib version 2.0.2

创建数据

这个简单的数据集包括了:1880年出生的,5个常用的婴儿的名字,以及对应的婴儿数量。

# 初始数据集: 婴儿名字和出生率
names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]

zip 函数将这两个列表合并在一起。

# 查看一下zip函数的帮助
zip?
BabyDataSet = list(zip(names, births))
BabyDataSet
[('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]

我们已经完成了一个基本的数据集的创建。 我们现在用 pandas 将这些数据导出到一个 csv 文件中。

df 是一个 DataFrame对象。 你可以把这个对象理解为包含了 BabyDataset 的内容而格式非常象一个 sql 表格或者 Excel 的数据表。 让我们看看 df 中的内容。

df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
df

<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Names</th>
<th>Births</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Bob</td>
<td>968</td>
</tr>
<tr>
<th>1</th>
<td>Jessica</td>
<td>155</td>
</tr>
<tr>
<th>2</th>
<td>Mary</td>
<td>77</td>
</tr>
<tr>
<th>3</th>
<td>John</td>
<td>578</td>
</tr>
<tr>
<th>4</th>
<td>Mel</td>
<td>973</td>
</tr>
</tbody>
</table>
</div>

将 dataframe 导出到一个 csv 文件中。 我们将导出文件命名为 births1880.csv。 导出 csv 文件的函数是 to_csv。 除非你指定了其他的文件目录,否则导出的文件将保存在和 notebook 文件相同的位置。

# 查看一下 to_csv 的帮助
df.to_csv?

我们会使用的参数是 indexheader。 将这两个参数设置为 False 将会防止索引(index)和列名(header names)被导出到文件中。 你可以试着改变这两个参数值来更好的理解这两个参数的作用。

df.to_csv('births1880.csv', index=False, header=False)

获取数据

我们将使用 pandas 的 read_csv 函数从 csv 文件中获取数据。 我们先看看这个函数的帮助以及它需要什么参数。

read_csv?

这个函数有很多的参数,但我们目前只需要文件的位置。

注意: 取决于你把 notebook 保存在什么位置,你也许需要修改一下文件的位置。

Location = r'./births1880.csv' #从 notebook 当前的位置读取 csv 文件
df = pd.read_csv(Location)

注意字符串之前的 r 。 因为斜线(slash)是一个特殊字符,在字符串之前放置前导的 r 将会把整个字符串进行转义(escape)。

df

<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Bob</th>
<th>968</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Jessica</td>
<td>155</td>
</tr>
<tr>
<th>1</th>
<td>Mary</td>
<td>77</td>
</tr>
<tr>
<th>2</th>
<td>John</td>
<td>578</td>
</tr>
<tr>
<th>3</th>
<td>Mel</td>
<td>973</td>
</tr>
</tbody>
</table>
</div>

这里出现了一个问题。 read_csv 函数将 csv 文件中的第一行作为了每列的列名(head names)。 这明显不对,因为数据文件没有提供列名。

要修正这个错误,我们需要给 read_csv 函数传入 header 这个参数,并设置为 None (Python中 null 的意思)。

相关文章

网友评论

      本文标题:2019-10-14

      本文链接:https://www.haomeiwen.com/subject/eijdmctx.html