热线电话:13121318867

登录
2018-12-19 阅读量: 643
为什么会丢失数据

有些人可能会对失踪的数据进行狡辩。“缺失”只是指 NA(“不可用”)或“因任何原因不存在”。许多数据集只是因缺少数据,因为它存在且未被收集或从未存在过。例如,在财务时间序列的集合中,某些时间序列可能在不同的日期开始。因此,开始日期之前的值通常会被标记为缺失。

在pandas中,将数据丢失引入数据集的最常见方法之一是重新索引。例如:

In [1]: df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],
...: columns=['one', 'two', 'three'])
...:

In [2]: df['four'] = 'bar'

In [3]: df['five'] = df['one'] > 0

In [4]: df
Out[4]:
one two three four five
a -0.166778 0.501113 -0.355322 bar False
c -0.337890 0.580967 0.983801 bar False
e 0.057802 0.761948 -0.712964 bar True
f -0.443160 -0.974602 1.047704 bar False
h -0.717852 -1.053898 -0.019369 bar False

In [5]: df2 = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

In [6]: df2
Out[6]:
one two three four five
a -0.166778 0.501113 -0.355322 bar False
b NaN NaN NaN NaN NaN
c -0.337890 0.580967 0.983801 bar False
d NaN NaN NaN NaN NaN
e 0.057802 0.761948 -0.712964 bar True
f -0.443160 -0.974602 1.047704 bar False
g NaN NaN NaN NaN NaN
h -0.717852 -1.053898 -0.019369 bar False
0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子