DataFrame入门-----python进行数据分析

DataFrame是一个表格型的数据结构,他含有一组有序的列,每列可以是不同的值类型。

DataFrame既有行索引也有列索引。

DataFrame中的数据块以一个或多个二维数据块存放的。

>>> data={'state':['a','b','c'],'year':[2000,2001,2002],'pop':[1.5,1.6,1.7]}
>>> DataFrame(data)
   pop state  year
0  1.5     a  2000
1  1.6     b  2001
2  1.7     c  2002
>>> DataFrame(data,columns=['year','pop'])
   year  pop
0  2000  1.5
1  2001  1.6
2  2002  1.7
>>> DataFrame(data,columns=['year','pop','nan'],index=[1,2,3])
   year  pop  nan
1  2000  1.5  NaN
2  2001  1.6  NaN
3  2002  1.7  NaN
>>> frame2=DataFrame(data,columns=['year','pop','nan'],index=[1,2,3])
>>> frame2['year']
1    2000
2    2001
3    2002
Name: year, dtype: int64
>>> frame2.year
1    2000
2    2001
3    2002
Name: year, dtype: int64
>>> frame2['debate']=16.5
>>> frame2
   year  pop  nan  debate
1  2000  1.5  NaN    16.5
2  2001  1.6  NaN    16.5
3  2002  1.7  NaN    16.5

pandas的索引对象负责管理轴标签和其他元数据,

构建Series或DataFrame时,所用到的任何数组或其他序列的标签都会被转换成一个Index

Index对象是不可修改的(immutable),因此用户不能对其进行修改,不可修改性非常重要,因为这样才能使Index对象在多个数据结果之间安全共享

>>> obj = Series(range(3),index = ['a','b','c'])
>>> index = obj.index
>>> index
Index([u'a', u'b', u'c'], dtype='object')
>>> index[1] = 's'
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "D:\python\lib\site-packages\pandas\core\indexes\base.py", line 2065, in __setitem__
    raise TypeError("Index does not support mutable operations")
TypeError: Index does not support mutable operations

由于开发人原的不断努力,Index可以被继承从而实现特别的轴索引功能

Index的功能也类似与一个特定大小的集合

每个索引都具有一些方法和属性,可用于设置逻辑和回答有关该索引所包含的数据的常见问题。

猜你喜欢

转载自blog.csdn.net/Da___Vinci/article/details/82984760