Pandas有两大数据结构:Series和DataFrame,之前已对Series对象进行了介绍(链接),本文主要对DataFrame对象的常用用法进行总结梳理。

约定:

  1. import pandas as pd

1、什么是DataFrame对象?

一个二维表,有行索引(index)和列索引(columns),列的数据类型可以不同。

2、DataFrame对象的创建

DataFrame对象的创建主要是使用pd.DataFrame方法。主要包括以下三种:

(1)方法1:通过等长列表组成的字典创建

  1. df1 = pd.DataFrame({'a':[1,2,3,4],'b':['w','x','y','z']})
  2. df1
  3. Out[205]:
  4. a b
  5. 0 1 w
  6. 1 2 x
  7. 2 3 y
  8. 3 4 z

(2)方法2:通过嵌套字典创建

外层字典的键作为列索引,内层字典的键作为行索引。

  1. df2 = pd.DataFrame({'a':{1:11,2:22,3:33},'b':{1:111,2:222,3:333,4:444}})
  2. df2
  3. Out[206]:
  4. a b
  5. 1 11.0 111
  6. 2 22.0 222
  7. 3 33.0 333
  8. 4 NaN 444

(3)方法3:通过numpy数组创建

注意传入DataFrame对象的形状。

  1. df3 = pd.DataFrame(np.arange(12).reshape(4,3))
  2. df3
  3. Out[211]:
  4. 0 1 2
  5. 0 0 1 2
  6. 1 3 4 5
  7. 2 6 7 8
  8. 3 9 10 11

##3、DataFrame对象的五个主要属性
DataFrame对象的五个主要属性:索引、值、名称、数据类型、形状。
###(1)索引
**a. 索引的查看**
**行**索引使用**index属性**,**列**索引使用**columns属性**,返回Index对象。

  1. df1.index
  2. Out[212]: RangeIndex(start=0, stop=4, step=1)
  3. df1.columns
  4. Out[213]: Index([u'a', u'b'], dtype='object')

索引可以有重复的,判断是否有重复索引,使用Index对象的is_unique属性判断。

  1. df1.index.is_unique
  2. Out[215]: True

b. 索引的修改

索引对象是一个不可变数组,不能修改其中的值。

  1. df1.index[1]=5
  2. Traceback (most recent call last):
  3. File "<ipython-input-217-360108374774>", line 1, in <module>
  4. df1.index[1]=5
  5. File "/usr/local/share/anaconda2/lib/python2.7/site-packages/pandas/indexes/base.py", line 1404, in __setitem__
  6. raise TypeError("Index does not support mutable operations")
  7. TypeError: Index does not support mutable operations

如果想修改索引,只能将其重定向到一个新的索引对象。

  1. df1.index=[5,6,7,8]
  2. df1
  3. Out[221]:
  4. a b
  5. 5 1 w
  6. 6 2 x
  7. 7 3 y
  8. 8 4 z

c. 索引的重排

使用reindex方法进行索引重排。通过index参数或者columns参数来区分是对行索引重排还是对列索引重排。重排产生一个新DataFrame对象。

  1. df1.reindex(index=[6,8,5,7])
  2. Out[222]:
  3. a b
  4. 6 2 x
  5. 8 4 z
  6. 5 1 w
  7. 7 3 y
  8. df1.reindex(columns=['b','a'])
  9. Out[223]:
  10. b a
  11. 5 w 1
  12. 6 x 2
  13. 7 y 3
  14. 8 z 4

可同时进行行、列索引的重排。

  1. df1.reindex(index=[6,8,5,7],columns=['b','a'])
  2. Out[224]:
  3. b a
  4. 6 x 2
  5. 8 z 4
  6. 5 w 1
  7. 7 y 3

索引重排可实现3个目的:

① 对现有索引进行顺序指定,即重新排列原来的元素顺序;

② 删除某个旧索引,即删除对应元素;

  1. df1.reindex(index=[6,8,7])
  2. Out[225]:
  3. a b
  4. 6 2 x
  5. 8 4 z
  6. 7 3 y

③ 增加某个新索引,即增加新元素,值为NaN。

  1. df1.reindex(index=[6,8,5,7,9])
  2. Out[226]:
  3. a b
  4. 6 2.0 x
  5. 8 4.0 z
  6. 5 1.0 w
  7. 7 3.0 y
  8. 9 NaN NaN

d. 索引的排序

使用sort_index方法根据索引进行升序、降序排列。

axis参数指定排序的方向:行内排序\列内排序。默认axis=0,列内排序;axis=1,行内排序。

  1. df4 = pd.DataFrame({'c':[11,33,22,44],'b':['w','x','y','z'],'a':[1,2,3,4]},index=[3,5,2,1],columns=['b','c','a'])
  2. df4
  3. Out[229]:
  4. b c a
  5. 3 w 11 1
  6. 5 x 33 2
  7. 2 y 22 3
  8. 1 z 44 4
  9. df4.sort_index()
  10. Out[231]:
  11. b c a
  12. 1 z 44 4
  13. 2 y 22 3
  14. 3 w 11 1
  15. 5 x 33 2
  16. df4.sort_index(axis=1)
  17. Out[232]:
  18. a b c
  19. 3 1 w 11
  20. 5 2 x 33
  21. 2 3 y 22
  22. 1 4 z 44

ascending参数指定升降序,取值为True或False,默认为True,升序排列。

e. 索引是否存在

使用in判断某索引是否存在。

  1. 2 in df1['b']
  2. Out[292]: True

(2)值

a. 值的查看

通过DataFrame对象的values属性获取元素的值,返回一个Numpy数组。

  1. df1.values
  2. Out[233]:
  3. array([[1, 'w'],
  4. [2, 'x'],
  5. [3, 'y'],
  6. [4, 'z']], dtype=object)

b. 值的修改

无法通过赋值对某一个元素进行取值就改。

  1. df1.values[1][1]='a'
  2. df1
  3. Out[243]:
  4. a b
  5. 0 1 w
  6. 1 2 x
  7. 2 3 y
  8. 3 4 z

只能对一行或者一列进行修改。

  1. df1['a']=55
  2. df1['b']=range(4)
  3. df1
  4. Out[245]:
  5. a b
  6. 0 55 0
  7. 1 55 1
  8. 2 55 2
  9. 3 55 3

c. 值的排序

使用sort_values方法根据值进行升序、降序排列。

by参数指定排序的行\列索引名,可按照多个索引进行排序,传入列表即可,索引顺序即为排序优先级。

  1. df4.sort_values(by='c')
  2. Out[250]:
  3. b c a
  4. 3 w 11 1
  5. 2 y 22 3
  6. 5 x 33 2
  7. 1 z 44 4
  8. df4.sort_values(by=['c','a'])
  9. Out[251]:
  10. b c a
  11. 3 w 11 1
  12. 2 y 22 3
  13. 5 x 33 2
  14. 1 z 44 4

axis参数指定排序的方向:行内排序\列内排序,默认axis=0,列内排序,axis=1,行内排序。

  1. df4.sort_values(by=3,axis=1)
  2. Out[252]:
  3. a c b
  4. 3 1 11 w
  5. 5 2 33 x
  6. 2 3 22 y
  7. 1 4 44 z

ascending参数指定升降序,取值为True或False,默认为True,升序排列。

d. 值的排名

使用rank方法,对于并列排名,默认取其均值。

  1. df4.rank()
  2. Out[253]:
  3. b c a
  4. 3 1.0 1.0 1.0
  5. 5 2.0 3.0 2.0
  6. 2 3.0 2.0 3.0
  7. 1 4.0 4.0 4.0

可通过设置axis参数,指定排名方向,默认列内排名,即axis=0,axis=1,行内排名。

  1. df4.rank(axis=1)
  2. Out[254]:
  3. b c a
  4. 3 3.0 2.0 1.0
  5. 5 3.0 2.0 1.0
  6. 2 3.0 2.0 1.0
  7. 1 3.0 2.0 1.0

ascending参数指定升降序,取值为True或False,默认为True,升序排列。

e. 值是否存在

使用isin方法判断,要求传入一个列表,返回一个布尔型Series对象。

  1. df1['b'].isin([2])
  2. Out[294]:
  3. 0 False
  4. 1 False
  5. 2 True
  6. 3 False
  7. 4 False
  8. Name: b, dtype: bool

(3)名称

DataFrame对象的索引对象有名称属性。

但是DataFrame对象没有名称属性。

  1. df4.name
  2. Traceback (most recent call last):
  3. File "<ipython-input-255-6af278e0d702>", line 1, in <module>
  4. df4.name
  5. File "/usr/local/share/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py", line 2744, in __getattr__
  6. return object.__getattribute__(self, name)
  7. AttributeError: 'DataFrame' object has no attribute 'name'

(4)数据类型

通过dtypes属性获取DataFrame对象每列的数据类型。

  1. df4.dtypes
  2. Out[257]:
  3. b object
  4. c int64
  5. a int64
  6. dtype: object

(5)形状

通过shape属性获取DataFrame对象的形状,返回值为一个元组。

  1. df4.shape
  2. Out[258]: (4, 3)

##4、元素的操作
###(1)元素选取
**a. 选取行**
**选取一行:**
① 按索引名称选取:使用loc[索引名称]

  1. df4.loc[1]
  2. Out[261]:
  3. b z
  4. c 44
  5. a 4
  6. Name: 1, dtype: object

② 按索引位置序号选取:使用iloc[索引位置序号]

  1. df4.iloc[1]
  2. Out[262]:
  3. b x
  4. c 33
  5. a 2
  6. Name: 5, dtype: object

选取多行:

① 按索引名称(列表)选取:使用loc[索引名称列表]

  1. df4.loc[[1,3]]
  2. Out[263]:
  3. b c a
  4. 1 z 44 4
  5. 3 w 11 1

② 按索引位置序号(切片)选取:使用iloc[索引位置序号切片]

  1. df4.iloc[1:3]
  2. Out[265]:
  3. b c a
  4. 5 x 33 2
  5. 2 y 22 3

b.选取列

选取一列:

直接使用索引名称。

  1. df4['a']
  2. Out[267]:
  3. 3 1
  4. 5 2
  5. 2 3
  6. 1 4
  7. Name: a, dtype: int64

选取多列:

直接使用索引名称组成的列表。

  1. df4[['a','b']]
  2. Out[268]:
  3. a b
  4. 3 1 w
  5. 5 2 x
  6. 2 3 y
  7. 1 4 z

c. 同时选取行和列

① 按索引名称选取:使用loc[行索引名称列表,列索引名称列表]。

  1. df4.loc[[1,3],['a','b']]
  2. Out[269]:
  3. a b
  4. 1 4 z
  5. 3 1 w

② 按索引位置序号选取:使用iloc[行索引位置序号切片,列索引位置序号切片]

  1. df4.iloc[1:3,0:1]
  2. Out[270]:
  3. b
  4. 5 x
  5. 2 y

(2)元素过滤

可直接使用基于值的比较运算条件产生的布尔型数组进行过滤。

  1. df1>2
  2. Out[272]:
  3. a b
  4. 0 True False
  5. 1 True False
  6. 2 True False
  7. 3 True True
  8. df1[df1>2]
  9. Out[273]:
  10. a b
  11. 0 55 NaN
  12. 1 55 NaN
  13. 2 55 NaN
  14. 3 55 3.0

(3)元素新增

a. 方法1:通过赋值新增

  1. df1['c']=20
  2. df1
  3. Out[275]:
  4. a b c
  5. 0 55 0 20
  6. 1 55 1 20
  7. 2 55 2 20
  8. 3 55 3 20

b. 方法2:通过索引重排新增

  1. df1=df1.reindex(index=[0,1,2,3,4])
  2. df1
  3. Out[277]:
  4. a b c
  5. 0 55.0 0.0 20.0
  6. 1 55.0 1.0 20.0
  7. 2 55.0 2.0 20.0
  8. 3 55.0 3.0 20.0
  9. 4 NaN NaN NaN

(4)元素删除

使用drop方法通过删除指定索引来删除元素。

通过axis参数确定是删除行还是删除列,默认删除行。

a. 一次删除一行/列

  1. df1.drop(1)
  2. Out[280]:
  3. a b c
  4. 0 55.0 0.0 20.0
  5. 2 55.0 2.0 20.0
  6. 3 55.0 3.0 20.0
  7. 4 NaN NaN NaN

b. 一次删除多行/列

  1. df1.drop(['a','c'],axis=1)
  2. Out[281]:
  3. b
  4. 0 0.0
  5. 1 1.0
  6. 2 2.0
  7. 3 3.0
  8. 4 NaN

注意:传入的索引值要与选定的axis方向一致。

(5)算术运算

DataFrame对象支持直接进行算术运算。

  1. df2+2
  2. Out[283]:
  3. a b
  4. 1 13.0 113
  5. 2 24.0 224
  6. 3 35.0 335
  7. 4 NaN 446

(6)判断是否有空值

使用isnull或者notnull方法判断是否有空值,返回一个布尔型DataFrame对象。

  1. df1.isnull()
  2. Out[297]:
  3. a b c
  4. 0 False False False
  5. 1 False False False
  6. 2 False False False
  7. 3 False False False
  8. 4 True True True
  9. df1.notnull()
  10. Out[298]:
  11. a b c
  12. 0 True True True
  13. 1 True True True
  14. 2 True True True
  15. 3 True True True
  16. 4 False False False

(7)缺失值处理

缺失值的处理主要有两种方法:填充和过滤。

a.填充

使用fillna方法进行空值填充,该方法产生新对象,不会修改原对象。

  1. df2=df2.reindex(index=[1,2,3,4,5])
  2. df2
  3. Out[304]:
  4. a b
  5. 1 11.0 111.0
  6. 2 22.0 222.0
  7. 3 33.0 333.0
  8. 4 NaN 444.0
  9. 5 NaN NaN
  10. df2.fillna(99)
  11. Out[308]:
  12. a b
  13. 1 11.0 111.0
  14. 2 22.0 222.0
  15. 3 33.0 333.0
  16. 4 99.0 444.0
  17. 5 99.0 99.0

b.过滤

使用dropna方法进行空值过滤,该方法产生新对象,不会修改原对象。

axis参数确定过滤行还是过滤列,默认axis=0过滤行,axis=1过滤列。

how参数控制过滤标准(有空值就丢弃(any)、全空值才丢弃(all)),默认过滤任何含有缺失值的行/列。

  1. df2.dropna()
  2. Out[305]:
  3. a b
  4. 1 11.0 111.0
  5. 2 22.0 222.0
  6. 3 33.0 333.0
  7. df2.dropna(axis=1)
  8. Out[306]:
  9. Empty DataFrame
  10. Columns: []
  11. Index: [1, 2, 3, 4, 5]
  12. df2.dropna(how='all')
  13. Out[307]:
  14. a b
  15. 1 11.0 111.0
  16. 2 22.0 222.0
  17. 3 33.0 333.0
  18. 4 NaN 444.0

(8)过滤重复值

使用duplicated方法返回布尔型Series对象,判断哪些行是重复值。

  1. df1
  2. Out[317]:
  3. a b c
  4. 0 55.0 0.0 20.0
  5. 1 55.0 1.0 20.0
  6. 2 55.0 2.0 20.0
  7. 3 55.0 3.0 20.0
  8. 4 NaN NaN NaN
  9. df1.duplicated()
  10. Out[318]:
  11. 0 False
  12. 1 False
  13. 2 False
  14. 3 False
  15. 4 False
  16. dtype: bool

使用drop_duplicates方法过滤其中的重复行,不修改原对象,而是产生一个没有重复行的新DataFrame对象。

  1. df1.drop_duplicates()
  2. Out[320]:
  3. a b c
  4. 0 55.0 0.0 20.0
  5. 1 55.0 1.0 20.0
  6. 2 55.0 2.0 20.0
  7. 3 55.0 3.0 20.0
  8. 4 NaN NaN NaN

默认判断全部列,即所有列都相同才叫重复,也可以指定要判断的一列或者多列(通过传入一个列表)。

  1. df1.duplicated(['a','c'])
  2. Out[319]:
  3. 0 False
  4. 1 True
  5. 2 True
  6. 3 True
  7. 4 False
  8. dtype: bool
  9. df1.drop_duplicates(['a','c'])
  10. Out[321]:
  11. a b c
  12. 0 55.0 0.0 20.0
  13. 4 NaN NaN NaN

(9)汇总统计

常规的统计方法:sum(求和)、mean(均值)、cumsum(累计求和)、count(非空计数),按列进行汇总。

  1. df1.sum()
  2. Out[322]:
  3. a 220.0
  4. b 6.0
  5. c 80.0
  6. dtype: float64
  7. df1.mean()
  8. Out[324]:
  9. a 55.0
  10. b 1.5
  11. c 20.0
  12. dtype: float64
  13. df1.cumsum()
  14. Out[325]:
  15. a b c
  16. 0 55.0 0.0 20.0
  17. 1 110.0 1.0 40.0
  18. 2 165.0 3.0 60.0
  19. 3 220.0 6.0 80.0
  20. 4 NaN NaN NaN
  21. df1.count()
  22. Out[323]:
  23. a 4
  24. b 4
  25. c 4
  26. dtype: int64

使用describe方法直接生成描述性统计结果。

a. 当元素的数据类型为数值型时,生成的结果包括:均值、最大值、最小值、标准差、元素个数、百分位数。

  1. df1.describe()
  2. Out[326]:
  3. a b c
  4. count 4.0 4.000000 4.0
  5. mean 55.0 1.500000 20.0
  6. std 0.0 1.290994 0.0
  7. min 55.0 0.000000 20.0
  8. 25% 55.0 0.750000 20.0
  9. 50% 55.0 1.500000 20.0
  10. 75% 55.0 2.250000 20.0
  11. max 55.0 3.000000 20.0

b. 当元素的数据类型为类别型时,生成的结果包括:唯一值个数、最大类别、最大类别频数。

  1. df5=pd.DataFrame({'a':['s','t','y','r'],'b':['w','x','y','z']})
  2. df5
  3. Out[330]:
  4. a b
  5. 0 s w
  6. 1 t x
  7. 2 y y
  8. 3 r z
  9. df5.describe()
  10. Out[331]:
  11. a b
  12. count 4 4
  13. unique 4 4
  14. top t w
  15. freq 1 1

(10)分组聚合

使用groupby方法产生一个GroupBy对象,然后使用聚合函数(mean、sum等)进行聚合计算。

groupby方法中传入划分组的一列或者多列。

  1. df1.groupby('a').sum()
  2. Out[337]:
  3. b c
  4. a
  5. 55.0 6.0 80.0

##5、DataFrame对象之间的操作
###(1)算术运算
通过+、-、*、\等运算符或者add、sub、mul、div等方法进行两个DataFrame对象之间的算术运算。
运算时,行、列索引同时对齐,对应元素进行算术运算,没有重叠的位置,运算结果为NaN。

  1. df1
  2. Out[338]:
  3. a b c
  4. 0 55.0 0.0 20.0
  5. 1 55.0 1.0 20.0
  6. 2 55.0 2.0 20.0
  7. 3 55.0 3.0 20.0
  8. 4 NaN NaN NaN
  9. df2
  10. Out[339]:
  11. a b
  12. 1 11.0 111.0
  13. 2 22.0 222.0
  14. 3 33.0 333.0
  15. 4 NaN 444.0
  16. 5 NaN NaN
  17. df1+df2
  18. Out[340]:
  19. a b c
  20. 0 NaN NaN NaN
  21. 1 66.0 112.0 NaN
  22. 2 77.0 224.0 NaN
  23. 3 88.0 336.0 NaN
  24. 4 NaN NaN NaN
  25. 5 NaN NaN NaN
  26. df1.add(df2)
  27. Out[341]:
  28. a b c
  29. 0 NaN NaN NaN
  30. 1 66.0 112.0 NaN
  31. 2 77.0 224.0 NaN
  32. 3 88.0 336.0 NaN
  33. 4 NaN NaN NaN
  34. 5 NaN NaN NaN

在使用上述方法进行算术运算时,可以使用fill_value参数进行空值填充,会先填充后进行算术运算。

  1. df1.add(df2,fill_value=0)
  2. Out[343]:
  3. a b c
  4. 0 55.0 0.0 20.0
  5. 1 66.0 112.0 20.0
  6. 2 77.0 224.0 20.0
  7. 3 88.0 336.0 20.0
  8. 4 NaN 444.0 NaN
  9. 5 NaN NaN NaN

(2)关联操作

使用pd.merge方法对两个DataFrame对象进行关联操作,本质上与两张数据库的二维表的join操作效果相同。

  1. df6=pd.DataFrame({'a':{1:11,2:22,3:33},'b':{1:111,2:222,3:333},'c':{1:11,3:33,5:55}})
  2. df6
  3. Out[347]:
  4. a b c
  5. 1 11.0 111.0 11.0
  6. 2 22.0 222.0 NaN
  7. 3 33.0 333.0 33.0
  8. 5 NaN NaN 55.0
  9. df7=pd.DataFrame({'b':{1:111,2:444,3:333},'c':{1:11,2:44,3:33}})
  10. df7
  11. Out[348]:
  12. b c
  13. 1 111 11
  14. 2 444 44
  15. 3 333 33
  16. pd.merge(df6,df7,on='b')
  17. Out[349]:
  18. a b c_x c_y
  19. 0 11.0 111.0 11.0 11
  20. 1 33.0 333.0 33.0 33

a. how参数决定关联方式,how='inner'相当于inner join,这是默认情况;how='left'相当于left join;how='right'相当于right join;how='outer'相当于cross join。

  1. pd.merge(df6,df7,on='b',how='left')
  2. Out[350]:
  3. a b c_x c_y
  4. 0 11.0 111.0 11.0 11.0
  5. 1 22.0 222.0 NaN NaN
  6. 2 33.0 333.0 33.0 33.0
  7. 3 NaN NaN 55.0 NaN

b. on参数决定关联条件。当两个DataFrame对象的关联列名相同时,使用on参数指定;当两个DataFrame对象的关联列名不同时,则使用left_on和right_on参数分别指定。

  1. pd.merge(df6,df7,left_on='a',right_on='c')
  2. Out[352]:
  3. a b_x c_x b_y c_y
  4. 0 11.0 111.0 11.0 111 11
  5. 1 33.0 333.0 33.0 333 33

c. suffixes参数处理合并后相同列名的命名问题,确定合并后的后缀。

  1. pd.merge(df6,df7,on='b',suffixes=['_left','_right'])
  2. Out[351]:
  3. a b c_left c_right
  4. 0 11.0 111.0 11.0 11
  5. 1 33.0 333.0 33.0 33

d. right_index参数,取值为True代表右侧DataFrame的行索引作为连接键参与连接。

e. left_index参数,取值为True代表左侧DataFrame的行索引作为连接键参与连接。

  1. pd.merge(df6,df7,left_on='a',right_index=True,how='left')
  2. Out[354]:
  3. a b_x c_x b_y c_y
  4. 1 11.0 111.0 11.0 NaN NaN
  5. 2 22.0 222.0 NaN NaN NaN
  6. 3 33.0 333.0 33.0 NaN NaN
  7. 5 NaN NaN 55.0 NaN NaN
  8. pd.merge(df6,df7,left_index=True,right_index=True)
  9. Out[355]:
  10. a b_x c_x b_y c_y
  11. 1 11.0 111.0 11.0 111 11
  12. 2 22.0 222.0 NaN 444 44
  13. 3 33.0 333.0 33.0 333 33

##6、DataFrame对象与Series对象之间的操作
###(1)算术运算
通过+、-、*、\等运算符或者add、sub、mul、div等方法进行DataFrame对象和Series对象之间的算术运算。
默认将Series的索引匹配DataFrame的列索引,然后沿着行一直向下传播。

  1. df8=pd.DataFrame({'a':{1:100,2:200,3:300},'b':{1:200,2:300,3:400},'c':{1:300,2:400,3:500}})
  2. df8
  3. Out[360]:
  4. a b c
  5. 1 100 200 300
  6. 2 200 300 400
  7. 3 300 400 500
  8. ser1=pd.Series([100,200,300],index=['a','b','c'])
  9. ser1
  10. Out[361]:
  11. a 100
  12. b 200
  13. c 300
  14. dtype: int64
  15. df8-ser1
  16. Out[362]:
  17. a b c
  18. 1 0 0 0
  19. 2 100 100 100
  20. 3 200 200 200
  21. df8.sub(ser1)
  22. Out[363]:
  23. a b c
  24. 1 0 0 0
  25. 2 100 100 100
  26. 3 200 200 200

使用上述方法时,可以使用axis参数来设定二者索引的匹配方向,是按行索引匹配(axis=0),还是按列索引匹配(axis=1,默认)。索引没有对齐的元素,运算结果为NaN。

  1. df8.sub(ser1,axis=1)
  2. Out[364]:
  3. a b c
  4. 1 0 0 0
  5. 2 100 100 100
  6. 3 200 200 200
  7. df8.sub(ser1,axis=0)
  8. Out[365]:
  9. a b c
  10. 1 NaN NaN NaN
  11. 2 NaN NaN NaN
  12. 3 NaN NaN NaN
  13. a NaN NaN NaN
  14. b NaN NaN NaN
  15. c NaN NaN NaN
  16. ser2=df8['a']
  17. ser2
  18. Out[368]:
  19. 1 100
  20. 2 200
  21. 3 300
  22. Name: a, dtype: int64
  23. df8.sub(ser2,axis=0)
  24. Out[369]:
  25. a b c
  26. 1 0 100 200
  27. 2 0 100 200
  28. 3 0 100 200

##7.参考与感谢
\[1] [利用Python进行数据分析](https://book.douban.com/subject/25779298/)

重拾Python(4):Pandas之DataFrame对象的使用的更多相关文章

  1. 重拾Python(3):Pandas之Series对象的使用

    Pandas是Python下最强大的数据分析和探索库,是基于Numpy库构建的,支持类似SQL的结构化数据的增.删.查.改,具有丰富的数据处理函数.Pandas有两大数据结构:Series和DataF ...

  2. pandas中DataFrame对象to_csv()方法中的encoding参数

    当使用pd.read_csv()方法读取csv格式文件的时候,常常会因为csv文件中带有中文字符而产生字符编码错误,造成读取文件错误,在这个时候,我们可以尝试将pd.read_csv()函数的enco ...

  3. 重拾Python(5):数据读取

    本文主要对Python如何读取数据进行总结梳理,涵盖从文本文件,尤其是excel文件(用于离线数据探索分析),以及结构化数据库(以Mysql为例)中读取数据等内容. 约定: import numpy ...

  4. 又见Python<3>:Pandas之Series对象的使用

    Pandas是Python下最强大的数据分析和探索库,是基于Numpy库构建的,支持类似SQL的结构化数据的增.删.查.改,具有丰富的数据处理函数.Pandas有两大数据结构:Series和DataF ...

  5. 将pandas的Dataframe对象读写Excel文件

    Dataframe对象生成Excel文件 需要xlrd库  命令  pip install xlrd #导入pandas import pandas as pd import numpy as np ...

  6. [译]从列表或字典创建Pandas的DataFrame对象

    原文来源:http://pbpython.com/pandas-list-dict.html 介绍 每当我使用pandas进行分析时,我的第一个目标是使用众多可用选项中的一个将数据导入Pandas的D ...

  7. Python: Pandas的DataFrame如何按指定list排序

    本文首发于微信公众号“Python数据之道”(ID:PyDataRoad) 前言 写这篇文章的起由是有一天微信上一位朋友问到一个问题,问题大体意思概述如下: 现在有一个pandas的Series和一个 ...

  8. 重拾python

    前一段碰到几次关于日期计算的题:给出一个日期,计算下一天的日期.虽然不限语言,可是我就C/C++还算熟悉,别的都是刚了解皮毛,根本不会用现成的库啊,无奈啊...只好用c语言一点点实现了,当时真是无比怀 ...

  9. 重拾Python(2):如何安装第三方库(Windows)

    使用python进行数据分析或者数据处理时,往往需要使用一些库,而使用库之前必须安装它.Anaconda内置了很多常用的第三方库,可以满足绝大部分需求,比如numpy.pandas.matplotli ...

随机推荐

  1. ES6 中 Promise 详解

    Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise 是一个对象,从它可以获取异步操作的消息.Promise 提供统一的 API ...

  2. 关闭系统邮件提醒:you hava a new mail(转)

    有时候,在输入某些触及到系统安全或者内核方面的命令都会提醒你: You have new mail in /var/spool/mail/root 只需要在root 用户下,不设置邮件检测即可! #e ...

  3. setContentView()与LayoutInflater.inflate()作用

    @Override protected void onCreate(Bundle savedInstanceState) {  try{   super.onCreate(savedInstanceS ...

  4. Ubuntu16.0.4的磁盘管理

    ubuntu下硬盘无损分区移动修改工具 原创 2014年04月13日 :: ubuntu上面其实有很好的分区调整工具,gparted,非常好使用 安装非常简单 sudo apt-get install ...

  5. 按指定id顺序查询

    SELECT a.p,* FROM tb1 t,( as p union as p union as p union as p union as p ) a where t.id=a.id order ...

  6. alpha-咸鱼冲刺day1

    一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 登陆界面随意写了一下.(明天用来做测试的) 把学姐给我的模板改成了自家的个人主页界面,侧边栏啥的都弄出来了(快撒花花!) 四,问题 ...

  7. GNU/Hurd笔记整理

    patch 0 关于文件锁支持的解决方案,大部分是由Neal Walfield在2001年完成的.这些补丁由Marcus Brinkmann发表,随后被Michael Banck于2002年修改了部分 ...

  8. 20145237《Java程序设计》第一周学习总结

    教材学习内容总结 java可分为Java SE.Java EE.Java ME三大平台. java SE分为JVM.JRE.JDK.与java语言四个部分. JRE包括java SE API和JVM. ...

  9. 手把手教你 LabVIEW 串口仪器控制——VISA 驱动下载安装篇

           仪器控制,核心在于 VISA 函数..有些仪器可能不需要 VISA,有自己的 DLL 什么的,我就管不着.        正常情况下,大家安装的 LabVIEW,都是不带 VISA 驱动 ...

  10. js的 == 和 ===的区别

    1.对于string,number等基础类型,==和===是有区别的 不同类型间比较,==之比较转化成同一类型后的值看值是否相等,===如果类型不同,其结果就是不等,同类型比较,直接进行"值 ...