panda2

pandas是python为数据分析建造的可靠工具,很多地方和R语言有想通之处。数据分析并不是工具越高深越好,excel,R,python都是针对不同情况的不同工具,各有各的优缺点,
就像你要搭一个架子,或者做一个工艺品,有的小锤子比较合适,有的就得用大斧子了。
excel其实是数据分析的强力武器。对于小数据量的情况下,excel有其先天的优势。而Python和R更像一个高性能的数据处理工具。
然而仅仅会使用各种厉害工具,数据落不了地是啥也没用的。落地,和业务贴合,永远是数据最终走向。近期转到研发部门,和业务贴合的机会少了,对这方面更有很多深刻感触。

下面这几点可能更像从excel角度去看python.
1.panda 的 index 更像横坐标 x,同时也可以把它当做一个list ,可以像数组一样赋值,取数。
2.两个跟属性判断相关的语句。in 是判断某个columns 或者 index中是否存在某个字段。 is 是判断数据格式类型。
3.另外基本功能,reindex 调整 index的工具函数。
基本格式:
obj.reindex(['a','b','c','d','e'],fill_value = 0.0)

功能:可以在横坐标和纵坐标上进行修改
frame = DataFrame(np.arange(9).reshape(3,3),index=['a','b','c'],columns=['Ohio','Texas','California'])
frame.reindex(['a','b','c','d'])
frame.reindex(columns=['Ohio','Texas','California','NewYork'])
4.取数据子矩阵 frame.ix 函数,同时还可以有reindex的功能。
frame.ix(['a','b','d'],states)
data.ix[['Colorado','Utah'],['three','four']]
5.现在已经隐约可以感觉到 python的两个子模块运算类似于线性代数了
所以 两个dataframe的结果十分像线性代数的结果。
df1+df2
df1.add(df2,fill_value=0)
df1.mul(df2,fill_value=0)
df1.div(df2,fill_value=0)
df1.sub(df2,fill_value=0)

详细数据如下
========================================================================================

'''panda's index objects are responsible for holding the axis labels,like series'''
import pandas as pd
obj = Series(range(3),index=['a','b','c'])
index = obj.index
index
index[1:]
'''index = immutable'''
index[1]='d'
'''so the index can be valued by function'''
index = pd.Index(np.arange(3))
obj2 = Series([1.5,-2.5,0],index=index)
obj2

''' evaluate the attribute of index 判断属性用Is,判断存不存在用in'''
obj2.index is index

'Ohio' in frame3.columns

'2002' in obj2.index

'''Essential functionality'''
'''reindexing'''
obj=Series([4.5,7.2,-5.3,3.6],index=['d','b','a','c'])
obj2=obj.reindex(['a','b','c','d','e'])
obj2
'''fill the missing data'''
obj.reindex(['a','b','c','d','e'],fill_value = 0.0)
'''ordering fill the missing data'''
obj3=Series(['blue','green','black'],index=[0,2,4])
obj3.reindex(np.arange(5),method='ffill')

'''reindex can be alter row,column and both in data frame'''
frame = DataFrame(np.arange(9).reshape(3,3),index=['a','b','c'],columns=['Ohio','Texas','California'])
frame.reindex(['a','b','c','d'])
frame.reindex(columns=['Ohio','Texas','California','NewYork'])

months = ['APR','MAY','JUN','JUL','AUG']
frame.reindex(columns=months)
label=['a','b','c','d','e']
states=['Ohio','Texas','California','NewYork']
'''reindex 仅对x-axis有效'''
frame.reindex(label,method='ffill')
'''取子矩阵'''
frame.ix(['a','b','d'],states)

'''dropping entries from axis'''
obj = Series(np.arange(5.),index=['a','b','c','d','e'])
new_obj = obj.drop('c')
new_obj


'''drop from data frame'''
data=DataFrame(np.arange(16).reshape(4,4),index=['Ohio','Colorado','Utah','NewYork'],columns=['one','two','three','four'])
'''drop from index'''
data.drop(['Colorado','Utah'])
'''drop from column'''
data.drop('two',axis=1)

'''index,selection,filtering'''
obj=Series(np.arange(4.),index=['a','b','c','d'])
'''index可以像数组一样,通过数字定位,index 定位,取一个数,一串数'''
obj['b']
obj[1]
obj[1:2]
obj[['a','c','d']]
obj[[1,3]]
obj[obj < 2]

obj['b':'c']=5

data=DataFrame(np.arange(16).reshape(4,4),index=['Ohio','Colorado','Utah','New York'],columns=['one','two','three','four'])
'''follow by columns,但只是单维度的'''
data['two']
data[['three','one']]
data.ix['Ohio']
data[data['three']>5]
data[:2]

'''把data小于5的赋值0'''
data[data<5]=0

'''按照位置选择值'''
data.ix['Colorado','two']
data.ix['Colorado',['two','three']]
data.ix[['Colorado','Utah'],['three','four']]
data.ix[2]
data.ix[:'Utah','two']
data.ix[:2,'two']
data.ix[data.three>5,:3]

'''reindex'''
data.ix[['Colorado','Utah'],[3,0,1]]

'''arithmetic and data alignment'''
s1=Series([7.3,-2.5,3.4,1.5],index=['a','c','d','e'])
s2=Series([-2.1,3.6,-1.5,4,3.1],index=['a','c','e','f','g'])
'''not overlap return NA'''
s1+s2
'''dataframe'''
df1=DataFrame(np.arange(9.).reshape(3,3),columns=list('bcd'),index=['Ohio','Texas','Colorado'])
df2=DataFrame(np.arange(12.).reshape(4,3),columns=list('bde'),index=['Utah','Ohio','Texas','Oregon'])

df1+df2
'''只要有一个为空,就是空'''
df1.add(df2,fill_value=0)
'''只要有一个有数,另外一个就设为0'''
'''reindex'''
df1.reindex(columns=df2.columns,fill_value=0)

df1 = DataFrame(np.arange(12.).reshape(3,4),columns=list('abcd'))
df2 = DataFrame(np.arange(20.).reshape(4,5),columns=list('abcde'))
df1.add(df2,fill_value=0)
df1.mul(df2,fill_value=0)
df1.div(df2,fill_value=0)
df1.sub(df2,fill_value=0)



posted on 2017-05-11 18:27  小麦粒  阅读(269)  评论(0编辑  收藏  举报

导航