reindex:重新索引

pandas对象有一个重要的方法reindex,作用:创建一个适应新索引的新对象

以Series为例

 >>> series_obj = Series([4.5,1.3,5,-5.5],index=('a','b','c','d'))
>>> series_obj
a 4.5
b 1.3
c 5.0
d -5.5
dtype: float64
>>> obj2 = series_obj.reindex(['a','b','c','e','f'])
>>> obj2
a 4.5
b 1.3
c 5.0
e NaN
f NaN
dtype: float64

重新索引的时候可以自动填充Nan值

 >>> obj3 = series_obj.reindex(['a','b','c','e','f'],fill_value='')
>>> obj3
a 4.5
b 1.3
c 5
e 0
f 0

对于时间序列这样的有序数据,重新索引可能需要做一些插值操作,reindex的method参数提供此功能。

method的可选选项有:

ffill或pad :前向填充或搬运值

bfill或backfill:后向填充或搬运值

不存在前向或后项的行自动填充Nan

 >>> obj4 = Series(['red','blue','green'],index=[0,2,4])
>>> obj4
0 red
2 blue
4 green
dtype: object
>>> obj4.reindex(range(6),method='ffill')
0 red
1 red
2 blue
3 blue
4 green
5 green
dtype: object

DataFrame的重新索引

只传入一个序列的时候,默认是重新索引“行”,可以用关键字参数来定义行索引(index)和列索引(columns)。

 >>> frame = DataFrame(np.arange(9).reshape((3,3)),index = ['a','b','c'],columns = ['Ohio','Texas',"Cali"])
>>> frame2 = frame.reindex(['a','b','c','d'])
>>> frame2
Ohio Texas Cali
a 0.0 1.0 2.0
b 3.0 4.0 5.0
c 6.0 7.0 8.0
d NaN NaN NaN >>> frame3 = frame.reindex(columns = ['Ohio','Texas','Cali','Wile'],index=['a','b','c','d'],fill_value=4)
>>> frame3
Ohio Texas Cali Wile
a 0 1 2 4
b 3 4 5 4
c 6 7 8 4
d 4 4 4 4
>>>

如果对DataFrame的行和列重新索引的时候,插值只能按行应用

如果利用ix的标签索功能,重新索引会变得更简洁

 >>> frame5 = frame.ix[['a','b','c','d'], ['Ohio','Texas','Cali','Wile']]
>>> frame5
Ohio Texas Cali Wile
a 0.0 1.0 2.0 NaN
b 3.0 4.0 5.0 NaN
c 6.0 7.0 8.0 NaN
d NaN NaN NaN NaN

drop:丢弃指定轴上的项

>>> obj = Series(np.arange(5),index=['a','b','c','d','e'])
>>> obj
a 0
b 1
c 2
d 3
e 4
dtype: int32
>>> new_obj = obj.drop('b')
>>> new_obj
a 0
c 2
d 3
e 4 >>> new_obj2 = obj.drop(['b','c'])
>>> new_obj2
a 0
d 3
e 4
dtype: int32
#dataframe
>>> frame = DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])
>>> frame
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
>>> new_frame = frame.drop('a')
>>> new_frame
one two three four
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
>>> new_frame2 = frame.drop(['two','four'],axis = 1)
>>> new_frame2
one three
a 0 2
b 4 6
c 8 10
d 12 14

索引、选取和过滤

Series的索引,既可以是类似NumPy数组的索引,也可以是自定义的index

>>> obj
a 0
b 1
c 2
d 3
e 4
dtype: int32
>>> obj['a']
0
>>> obj[1]
1
注意:利用标签的切片运算,标签的右侧是封闭区间的,即包含末端。
>>> obj['a':'c']
a 0
b 1
c 2
dtype: int32
>>> obj[3:4]
d 3
dtype: int32
>>> obj[2:3]
c 2
dtype: int32
>>> obj[[3,1]]
d 3
b 1
dtype: int32
>>> obj[['a','c']]
a 0
c 2
dtype: int32
>>>

通过索引修改值

>>> obj[['b','d']] *=2
>>> obj
a 0
b 2
c 2
d 6
e 4
dtype: int32

dataframe的索引:

通过直接索引只能获取列

>>> frame
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
>>> frame['a']
KeyError: 'a'
>>> frame['one']
a 0
b 4
c 8
d 12
Name: one, dtype: int32
>>> frame[['one','four']]
one four
a 0 3
b 4 7
c 8 11
d 12 15

通过切片或布尔型数组,选取的是行

>>> frame[1:3] #不闭合区间
one two three four
b 4 5 6 7
c 8 9 10 11
>>> frame[frame['three'] > 8]
one two three four
c 8 9 10 11
d 12 13 14 15
>>>

DataFrame的索引字段ix

>>> frame.ix['a'] #按照行索引
one 0
two 1
three 2
four 3
Name: a, dtype: int32
>>> frame.ix[['b','d']]
one two three four
b 4 5 6 7
d 12 13 14 15
>>> frame.ix[1]#同样是按照行索引
one 4
two 5
three 6
four 7
Name: b, dtype: int32
>>> frame.ix[1:3]
one two three four
b 4 5 6 7
c 8 9 10 11
>>> frame.ix[1:2,[2,3,1]]
three four two
b 6 7 5
>>> frame.ix[1:3,[2,3,1]]
three four two
b 6 7 5
c 10 11 9
>>> frame.ix[['b','d'],['one','three']]
one three
b 4 6
d 12 14
>>> frame.ix[['b','d'],[3,1,2]]
four two three
b 7 5 6
d 15 13 14
>>> frame.ix[:,[2,3,1]]# 选取所有行
three four two
a 2 3 1
b 6 7 5
c 10 11 9
d 14 15 13

>>> frame.ix[frame.three >5,:3]
one two three
b 4 5 6
c 8 9 10
d 12 13 14

算术运算和数据对齐

>>> s1 = Series([1.3,4.5,6.6,3.4],index=['a','b','c','d'])
>>> s2 = Series([1,2,3,4,5,6,7],index=['a','b','c','d','e','f','g'])
>>> s1+s2
a 2.3
b 6.5
c 9.6
d 7.4
e NaN
f NaN
g NaN
dtype: float64
#不重叠的索引处引入缺失值
#DataFrame也是同理

再算术方法中填充缺失值

>>> df1 = DataFrame(np.arange(12).reshape((3,4)),columns=list('abcd'))
>>> df2 = DataFrame(np.arange(20).reshape((4,5)),columns=list('abcde'))
>>> df1+df2#普通的算术运算会产生缺失值
a b c d e
0 0.0 2.0 4.0 6.0 NaN
1 9.0 11.0 13.0 15.0 NaN
2 18.0 20.0 22.0 24.0 NaN
3 NaN NaN NaN NaN NaN
#用算术运算方法,可以填充缺失值
>>> df1.add(df2,fill_value=0)
a b c d e
0 0.0 2.0 4.0 6.0 4.0
1 9.0 11.0 13.0 15.0 9.0
2 18.0 20.0 22.0 24.0 14.0
3 15.0 16.0 17.0 18.0 19.0
>>>

算术运算方法有

add 加法

sub 减法

div 除法

mul 乘法

DataFrame和Series之间的运算

>>> frame
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
>>> series = frame.ix[0]
>>> series
one 0
two 1
three 2
four 3
Name: a, dtype: int32
>>> frame - series
one two three four
a 0 0 0 0
b 4 4 4 4
c 8 8 8 8
d 12 12 12 12
>>>

两者之间的运算会将Series的索引匹配到DataFrame的列,然后沿着行一直向下广播。

如果某个索引值在DataFrame的列或Series的索引中找不到,则参与运算的连个对象就会被重新索引以形成并集。

>>> series2 = Series(range(3),index = ['two','four','five'])
>>> frame +series2
five four one three two
a NaN 4.0 NaN NaN 1.0
b NaN 8.0 NaN NaN 5.0
c NaN 12.0 NaN NaN 9.0
d NaN 16.0 NaN NaN 13.0

如果希望匹配行,且在列上传播,则必须使用算术方法

>>> series3 = frame['two']
>>> frame.sub(series3,axis = 0)
one two three four
a -1 0 1 2
b -1 0 1 2
c -1 0 1 2
d -1 0 1 2
>>>

pandas(一)操作Series和DataFrame的基本功能的更多相关文章

  1. pandas基础:Series与DataFrame操作

    pandas包 # 引入包 import pandas as pd import numpy as np import matplotlib.pyplot as plt Series Series 是 ...

  2. pandas 的数据结构Series与DataFrame

    pandas中有两个主要的数据结构:Series和DataFrame. [Series] Series是一个一维的类似的数组对象,它包含一个数组数据(任何numpy数据类型)和一个与数组关联的索引. ...

  3. 02. Pandas 1|数据结构Series、Dataframe

    1."一维数组"Series Pandas数据结构Series:基本概念及创建 s.index  . s.values # Series 数据结构 # Series 是带有标签的一 ...

  4. numpy中的ndarray与pandas中的series、dataframe的转换

    一个ndarray是一个多维同类数据容器.每一个数组有一个dtype属性,用来描述数组的数据类型. Series是一种一维数组型对象,包含了一个值序列,并且包含了数据标签----索引(index). ...

  5. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  6. Python之Pandas中Series、DataFrame实践

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  7. pandas-06 Series和Dataframe的排序操作

    pandas-06 Series和Dataframe的排序操作 对pandas中的Series和Dataframe进行排序,主要使用sort_values()和sort_index(). DataFr ...

  8. 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作

    一.reindex() 方法:重新索引 针对 Series   重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...

  9. pandas 的数据结构(Series, DataFrame)

    Pandas 讲解 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的. Pandas 纳入了大量库和一些标 ...

随机推荐

  1. 论C++STL源代码中关于堆算法的那些事

    关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比較靠上的O(nlogn)常常会拿来和高速排序和归并排序讨论,并且它还有个长处是它的空间复杂度为O(1), 可是STL中没有给我们提供 ...

  2. Atitit.论垃圾文件的识别与清理 文档类型垃圾文件 与api概要设计pa6.doc

    Atitit.论垃圾文件的识别与清理 文档类型垃圾文件 与api概要设计pa6.doc 1. 俩个问题::识别垃圾文件与清理策略1 1.1. 文件类型:pic,doc,v,m cc,isho pose ...

  3. flink-connector-kafka consumer的topic分区分配源码

    转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/7200599.html flink官方提供了连接kafka的connector实现,由于调试的时候发 ...

  4. Django学习之URLconf

    Django处理request的步骤: 1.确定根URLconf 2.载入urls.py,找到变量urlpatterns,urlpatterns是django.conf.urls.url()的实例对象 ...

  5. iOS swift cookie创建存储移除

    保存网络请求的cookies,并存储到UserDefaults中 //保存COOKIES static func saveCookies(for urlStr: String){ if let url ...

  6. Java并发编程(二)为什么需要多线程

    如果不考虑多线程的话,那么在程序只有一条执行路径,代码串行执行:顺序执行.选择或者循环.单线程就像你用你惯常的手去写字,多线程编程就要求你左手画圆,右手画方.一不留神就会手忙脚乱,圆不是圆,方也不像方 ...

  7. IDEA 2017破解 license server激活

    确保电脑在联网状态,在激活窗口选择license server 填入下面的license server: http://intellij.mandroid.cn/ http://idea.imsxm. ...

  8. LeetCode543. Diameter of Binary Tree

    Description Given a binary tree, you need to compute the length of the diameter of the tree. The dia ...

  9. 第一百九十节,jQuery,编辑器插件

    jQuery,编辑器插件 学习要点: 1.编辑器简介 2.引入 uEditor 编辑器(Editor),一般用于类似于 word 一样的文本编辑器,只不过是编辑为 HTML 格式的.分类纯 JS 类型 ...

  10. PPT常用操作笔记

    1. 组合就是框成一组 2. 页与页之间的播放运作及效果设置“切换”,页内的元素过滤设置“动画” 3. 转PDF:PDF打印机或直接来个金山WPS 4. 讲义打印,要点:灰度.去深色背景.多页,弄了个 ...