亲宝软件园·资讯

展开

浅析pandas 数据结构中的DataFrame

人气:0

DataFrame 类型类似于数据库表结构的数据结构,其含有行索引和列索引,可以将DataFrame 想成是由相同索引的Series组成的Dict类型。在其底层是通过二维以及一维的数据块实现。

1. DataFrame 对象的构建

  1.1 用包含等长的列表或者是NumPy数组的字典创建DataFrame对象

In [68]: import pandas as pd

In [69]: from pandas import Series,DataFrame

# 建立包含等长列表的字典类型
In [70]: data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],'year': [2000, 2001, 20
 ...: 02, 2001, 2002],'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
In [71]: data
Out[71]: 
{'pop': [1.5, 1.7, 3.6, 2.4, 2.9],
 'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
 'year': [2000, 2001, 2002, 2001, 2002]}
# 建立DataFrame对象
In [72]: frame1 = DataFrame(data)
# 红色部分为自动生成的索引
In [73]: frame1
Out[73]: 
 pop state year
0 1.5 Ohio 2000
1 1.7 Ohio 2001
2 3.6 Ohio 2002
3 2.4 Nevada 2001
4 2.9 Nevada 2002

  在建立过程中可以指点列的顺序:

In [74]: frame1 = DataFrame(data,columns=['year', 'state', 'pop'])

In [75]: frame1
Out[75]: 
 year state pop
0 2000 Ohio 1.5
1 2001 Ohio 1.7
2 2002 Ohio 3.6
3 2001 Nevada 2.4
4 2002 Nevada 2.9

  和Series一样,DataFrame也是可以指定索引内容:

In [76]: ind = ['one', 'two', 'three', 'four', 'five']
In [77]: frame1 = DataFrame(data,index = ind)

In [78]: frame1
Out[78]: 
  pop state year
one 1.5 Ohio 2000
two 1.7 Ohio 2001
three 3.6 Ohio 2002
four 2.4 Nevada 2001
five 2.9 Nevada 2002

  1.2. 用由字典类型组成的嵌套字典类型来生成DataFrame对象

  当由嵌套的字典类型生成DataFrame的时候,外部的字典索引会成为列名,内部的字典索引会成为行名。生成的DataFrame会根据行索引排序

In [84]: pop = {'Nevada': {2001: 2.4, 2002: 2.9},'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}

In [85]: frame3 = DataFrame(pop)

In [86]: frame3
Out[86]: 
  Nevada Ohio
2000  NaN 1.5
2001  2.4 1.7
2002  2.9 3.6

  除了使用默认的按照行索引排序之外,还可以指定行序列:

In [95]: frame3 = DataFrame(pop,[2002,2001,2000])

In [96]: frame3
Out[96]: 
  Nevada Ohio
2002  2.9 3.6
2001  2.4 1.7
2000  NaN 1.5

  1.3 其它构造方法:

  

2. DataFrame 内容访问

  从DataFrame中获取一列的结果为一个Series,可以通过以下两种方式获取:

# 以字典索引方式获取
In [100]: frame1["state"]
Out[100]: 
one  Ohio
two  Ohio
three  Ohio
four  Nevada
five  Nevada
Name: state, dtype: object
# 以属性方式获取
In [101]: frame1.state
Out[101]: 
one  Ohio
two  Ohio
three  Ohio
four  Nevada
five  Nevada
Name: state, dtype: object

  也可以通过ix获取一行数据:

In [109]: frame1.ix["one"] # 或者是 frame1.ix[0]
Out[109]: 
pop  1.5
state Ohio
year  2000
Name: one, dtype: object
# 获取多行数据
In [110]: frame1.ix[["tow","three","four"]]
Out[110]: 
  pop state year
tow NaN  NaN  NaN
three 3.6 Ohio 2002.0
four 2.4 Nevada 2001.0
# 还可以通过默认数字行索引来获取数据
In [111]: frame1.ix[range(3)]
Out[111]: 
  pop state year
one 1.5 Ohio 2000
two 1.7 Ohio 2001
three 3.6 Ohio 2002

  获取指定行,指定列的交汇值:

In [119]: frame1["state"]
Out[119]: 
one  Ohio
two  Ohio
three  Ohio
four  Nevada
five  Nevada
Name: state, dtype: object

In [120]: frame1["state"][0]
Out[120]: 'Ohio'

In [121]: frame1["state"]["one"]
Out[121]: 'Ohio'

  先指定列再指定行:

In [125]: frame1.ix[0]
Out[125]: 
pop  1.5
state Ohio
year  2000
Name: one, dtype: object

In [126]: frame1.ix[0]["state"]
Out[126]: 'Ohio'

In [127]: frame1.ix["one"]["state"]
Out[127]: 'Ohio'

In [128]: frame1.ix["one"][0]
Out[128]: 1.5

In [129]: frame1.ix[0][0]
Out[129]: 1.5

3. DataFrame 对象的修改

  增加一列,并所有赋值为同一个值:

# 增加一列值
In [131]: frame1["debt"] = 10

In [132]: frame1
Out[132]: 
  pop state year debt
one 1.5 Ohio 2000 10
two 1.7 Ohio 2001 10
three 3.6 Ohio 2002 10
four 2.4 Nevada 2001 10
five 2.9 Nevada 2002 10

# 更改一列的值
In [133]: frame1["debt"] = np.arange(5)

In [134]: frame1
Out[134]: 
  pop state year debt
one 1.5 Ohio 2000  0
two 1.7 Ohio 2001  1
three 3.6 Ohio 2002  2
four 2.4 Nevada 2001  3
five 2.9 Nevada 2002  4

  追加类型为Series的一列

# 判断是否为东部区
In [137]: east = (frame1.state == "Ohio")

In [138]: east
Out[138]: 
one  True
two  True
three  True
four  False
five  False
Name: state, dtype: bool
# 赋Series值
In [139]: frame1["east"] = east

In [140]: frame1
Out[140]: 
  pop state year debt east
one 1.5 Ohio 2000  0 True
two 1.7 Ohio 2001  1 True
three 3.6 Ohio 2002  2 True
four 2.4 Nevada 2001  3 False
five 2.9 Nevada 2002  4 False

DataFrame 的行可以命名,同时多列也可以命名:

In [145]: frame3.columns.name = "state"

In [146]: frame3.index.name = "year"

In [147]: frame3
Out[147]: 
state Nevada Ohio
year    
2002  2.9 3.6
2001  2.4 1.7
2000  NaN 1.5

总结

以上所述是小编给大家介绍的pandas 数据结构之DataFrame,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论