继上一篇文章,这篇文章介绍一下Pandas模块里面的DataFrame结构html
DataFrame unifies two or more Series into a single data structure.Each Series then represents a named column of the DataFrame, and instead of each column having its own index, the DataFrame provides a single index and the data in all columns is aligned to the master index of the DataFrame.
这段话的意思是,DataFrame提供的是一个相似表的结构,由多个Series组成,而Series在DataFrame中叫columns(理解有错请指出,(逃~
数组
pd.DataFrame()
参数:
一、二维array;
二、Series 列表;
三、value为Series的字典;app
import pandas as pd import numpy as np s1=np.array([1,2,3,4]) s2=np.array([5,6,7,8]) df=pd.DataFrame([s1,s2]) print df
import pandas as pd import numpy as np s1=pd.Series(np.array([1,2,3,4])) s2=pd.Series(np.array([5,6,7,8])) df=pd.DataFrame([s1,s2]) print df
import pandas as pd import numpy as np s1=pd.Series(np.array([1,2,3,4])) s2=pd.Series(np.array([5,6,7,8])) df=pd.DataFrame({"a":s1,"b":s2}); print df
注:若建立使用的参数中,array、Series长度不同时,对应index的value值若不存在则为NaNide
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) df.ix[df.A>1,'B']= -1 print df
df.ix[条件,then操做区域]spa
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) df["then"]=np.where(df.A<3,1,0) print df
np.where(条件,then,else)code
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) df=df[df.A>=2] print df
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) df=df.loc[df.A>2] print df
(还有不少种方法就不一一列举了)htm
df = pd.DataFrame({'animal': 'cat dog cat fish dog cat cat'.split(), 'size': list('SSMMMLL'), 'weight': [8, 10, 11, 1, 20, 12, 12], 'adult' : [False] * 5 + [True] * 2}); #列出动物中weight最大的对应size group=df.groupby("animal").apply(lambda subf: subf['size'][subf['weight'].idxmax()]) print group
e.2 使用get_group 取出其中一分组get
df = pd.DataFrame({'animal': 'cat dog cat fish dog cat cat'.split(), 'size': list('SSMMMLL'), 'weight': [8, 10, 11, 1, 20, 12, 12], 'adult' : [False] * 5 + [True] * 2}); group=df.groupby("animal") cat=group.get_group("cat") print cat
http://pandas.pydata.org/pandas-docs/stable/cookbook.htmlpandas