随书练习,第五章 pandas入门2
# coding: utf-8 # In[1]: from pandas import Series,DataFrame import pandas as pd import numpy as np # In[2]: obj = Series(range(5),index=['a','a','b','b','c']) # In[3]: obj #带有重复索引的Series # In[4]: obj.index.is_unique # In[5]: obj['a'] # In[6]: obj['c'] # In[7]: df = DataFrame(np.random.randn(4,3),index=['a','a','b','b']) # In[8]: df # In[9]: df.loc['b'] # In[10]: df = DataFrame([[1.4,np.nan],[7.1,-4.5],[np.nan,np.nan],[0.75,-1.3]],index=['a','b','c','d'],columns=['one','two']) # In[11]: df # In[12]: df.sum() # In[13]: df.sum(axis=1) #自动忽略缺失值 # In[14]: df.mean(axis=1,skipna=False) #禁用自动忽略缺失值功能 # In[15]: df.idxmax() #间接统计,返回索引 # In[16]: df.idxmax(axis=1) # In[17]: df.cumsum() #累计型 # In[18]: df.describe() #数值型数据 # In[19]: obj = Series(['a','a','b','c']*4) # In[20]: obj # In[21]: obj.describe() #非数值型数据 # In[22]: # corr ,cov ,corrwith # In[23]: obj = Series(['c','a','d','a','a','b','b','c','c']) # In[24]: uniques = obj.unique() #这里只是找出唯一值,不排序,numpy有一个函数unique()去重并排序 # In[25]: uniques # In[26]: uniques.sort() # In[27]: uniques # In[28]: obj.value_counts() #用于计算各值出现的频数,降序排列 # In[29]: pd.value_counts(obj.values,sort=False) # In[30]: mask = obj.isin(['b','c']) # In[31]: mask # In[32]: obj[mask] #布尔型索引 # In[33]: data = DataFrame({'Qu1':[1,3,4,3,4],'Qu2':[2,3,1,2,3],'Qu3':[1,5,2,4,4]}) # In[34]: data # In[35]: result = data.apply(pd.value_counts).fillna(0) # In[36]: result #索引是唯一值,列是唯一值出现的次数 # In[37]: string_data = Series(['aardvark','artichoke',np.nan,'avocado']) # In[38]: string_data # In[39]: string_data.isnull() # In[40]: string_data[0] = None # In[41]: string_data.isnull() #python内置的None值也会被当作NA值处理 # In[42]: data = DataFrame({'Qu1':[np.nan,3,np.nan,3,4],'Qu2':[2,3,np.nan,2,3],'Qu3':[np.nan,5,np.nan,4,np.nan]}) # In[43]: data # In[44]: data.dropna() #默认删除有缺失值的数据 # In[45]: data.dropna(how='all') # In[46]: data.dropna(thresh=2) #缺失数量大于等于2的行删掉 # In[47]: data.notnull() #isnull的否定式 # In[48]: from numpy import nan as NA # In[49]: data = Series([1,NA,3.5,NA,7]) # In[50]: data.dropna() # In[51]: data[data.notnull()] #布尔型索引 # In[52]: data = DataFrame([[1,6.5,3],[1,NA,NA],[NA,NA,NA],[NA,6.5,3]]) # In[53]: cleaned = data.dropna() # In[54]: data # In[55]: cleaned # In[56]: data[4] = NA # In[57]: data # In[58]: data.dropna(axis=1,how='all') # In[59]: df = DataFrame(np.random.randn(7,3)) # In[60]: df.iloc[:4,1] = NA;df.iloc[:2,2] = NA # In[61]: df # In[62]: df.fillna(0) # In[63]: df.fillna({1:0.5,3:-1}) #可以对不同的列填充不同的值 # In[64]: _ = df.fillna(0,inplace=True) #会改变原数据 # In[65]: df # In[66]: df = DataFrame(np.random.randn(6,3)) # In[67]: df.iloc[2:,1] = NA;df.iloc[4:,2] = NA # In[68]: df # In[69]: df.fillna(method='ffill') # In[70]: df.fillna(method='ffill',limit=2) # In[71]: data = Series([1,NA,3.5,NA,7]) # In[72]: data.fillna(data.mean()) # In[73]: df # In[74]: df.mean() # In[75]: df.fillna(df.mean()) #用平均值填充 # In[76]: df.iloc[3,1]=5 # In[77]: df # In[78]: df.interpolate() #插值法填充缺失值 # In[79]: data = Series(np.random.randn(10),index=[['a','a','a','b','b','b','c','c','d','d'],[1,2,3,1,2,3,1,2,2,3]]) #层次化索引 # In[80]: data # In[81]: data.index # In[82]: data['b'] # In[83]: data['b':'c'] # In[84]: data[['b','d']] # In[85]: data.loc[['b','d']] # In[86]: data[:,2] # In[87]: data.unstack() # In[88]: data.unstack().stack() # In[89]: frame = DataFrame(np.arange(12).reshape((4,3)),index=[['a','a','b','b'],[1,2,1,2]],columns=[['Ohio','Ohio','Colorado'],['Green','Red','Green']]) # In[90]: frame # In[91]: frame.index.names = ['key1','key2'] # In[92]: frame.columns.names = ['state','color'] # In[93]: frame # In[94]: frame['Ohio'] # In[103]: pd.MultiIndex.from_arrays([['Ohio','Ohio','Colorado'],['Green','Red','Green']],names=['state','color']) # In[104]: frame.swaplevel('key1','key2') #互换级别 # In[106]: frame.sort_index(level=1) # In[109]: frame.swaplevel(0,1).sort_index(level=0) # In[110]: frame # In[111]: frame.sum(level='key2') # 根据级别进行汇总 # In[112]: frame.sum(level='color',axis=1) # In[113]: frame = DataFrame({'a':range(7),'b':range(7,0,-1),'c':['one','one','one','two','two','two','two'],'d':[0,1,2,0,1,2,3]}) # In[114]: frame # In[115]: frame2 = frame.set_index(['c','d']) #将一个或多个列转换为行索引 # In[116]: frame2 # In[117]: frame.set_index(['c','d'],drop=False) # In[118]: frame2 # In[119]: frame2.reset_index() # In[125]: frame2 # In[124]: frame.loc[frame.d>0]