pandas采用了很多Numpy的代码风格。
Numpy适合处理同质型的数值类数组数据,更适合进行数学那类的运算。
Pandas用来处理表格型或异质型数据,数据清洗和分析的主要工具。
以上两条纯属猜测
pandas的两个常用的数据结构:Series和DataFrame
import pandas as pd
from pandas import Series , DataFrame
一、Series

index 索引 values 值
简单的运算


'a' in obj ----> True 'a'为索引
我们可以直接将字典传递给Series
dic={'a':1,'b':2,'c':3,'d':4}
obj1=pd.Series(dic)


上面的例子中,三个值被放在正确的位置上,但是因为index ‘e’并没有出现在obj1的键中,所以它对应的值为NaN(not a number)。
以下是两个Series的拼接:

介绍两个函数 isnull notnull
pd.isnull(obj) pd.notnull(obj)

Series对象自身和其索引都有name属性,这个特性与pandas其他重要功能集成在一起:obj.name='test' obj.index.name='abcd'

Series的索引可以通过按位置赋值的方式进行改变: obj.index=['a','s','d','f']

二、DataFrame
DataFrame表示的是矩阵的数据表,它包含已排序的列集合,每一列可以是不同的值类型(数值、字符串、布尔值等)。DataFrame既有行索也有列索
字典转化DataFrame

head()方法

用字典创建DataFrame并对列进行选择,pd.DataFrame(dic,columns=['xxx','xx'])

用字典创建DataFrame并对index索引进行选择,pd.DataFrame(dic,index=['xxx','xx'])

这里需要注意一点,columns里的参数可以多于或者少于字典里的个数,但是index参数的个数必须和原字典里的键值个数相同。
列的索引
frame['age'] <==> frame.age 其实并不完全等价,区别将在后一个黑体注意中说明!!!!! 尽量使用frame['age']这种方式,没毛病。

行的索引 loc iloc

DataFrame的转置:

赋值:

给DataFrame的列赋值Series

frame2['result']=frame2.name=='b'

注意:这里的frame2['result'] 并不能用frame2.result代替,因为result列并不存在
del 删除列

将包含字典的嵌套字典赋值给DataFrame

查看DataFrame所有的列、行
frame2.columns frames.index

frame2.values

网友评论