python--Numpy and Pandas 笔记01


1
import numpy as np 2 import pandas as pd 3 from pandas import Series,DataFrame 4 5 #Series 6 s1 = Series([1,2,3], index=['A','B','C']) 7 s2 = Series([4,5,6,7], index=['B','C','D','E']) 8 9 s1 + s2 10 # 结果:A NaN 11 #   B 6.0 12 #    C 8.0 13 #    D NaN 14 #   E NaN 15 #   dtype: float64 16 # 对应项相加,其他为nan 17 18 #DataFrame 19 df1 = DataFrame(np.arange(4).reshape(2,2),index=['A','B'],columns=['BJ','SH']) 20 df2 = DataFrame(np.arange(9).reshape(3,3),index=['A','B','C'],columns=['BJ','SH','GZ']) 21 df1 + df2 22 #结果: BJ GZ SH 23 #   A 0.0 NaN 2.0 24 #   B 5.0 NaN 7.0 25 #   C NaN NaN NaN 26 27 df3 = Datadf3 = DataFrame([[1,2,3],[4,5,np.nan],[7,8,9]],index=['A','B','C'],columns=['c1','c2','c3']) 28 ''' 29 c1 c2 c3 30 A 1 2 3.0 31 B 4 5 NaN 32 C 7 8 9.0 33 ''' 34 df3.sum() 35 #结果:c1 12.0 36 #   c2 15.0 37 #   c3 12.0 38 #   dtype: float64 39 #这里的nan与实数相运算并不返回nan 40 df3.sum(axis=1) #则求得每一行的和,即ABC,由于默认axis=0,所以不写表示求的列 41 42 df3.min() #求最小值,max同理。整体同理与sum,不考虑nan 43 44 df3.describe() #统计内部数据 45 46 47 # 排序功能 48 #注:randn:正态分布 49 rand:0到1
50 #Series: 51 s1 = Series(np.random.randn(10)) 52 s2 = s1.sort_values() # 根据values排序 53 # 默认参数ascending=True,升序为True,倒序可以改参数sacending=False 54 s2.sort_index() # 根据index升序排列
55 #DataFrame: 56 df1 = DataFrame(np.random.randn(40).reshape(8,5),columns=['A','B','C','D','E']) 57 df1['A'].sort_values() #仅仅是对着一列排序,若想整体根据这一列进行排序要多df1进行排序 58 df1.sort_values('A') #则为全部排序,默认升序 59 60 df1.sort_values('A')[['A','D']] # 根据某列排序并输出所需要的几列 61 62 63 #重命名 64 df1.index = df1.index.map(str) #修改dataframe的index 65 df1.rename(index=str.upper,columns=str.lower) #通过map函数改变整个 66 df1.rename(index={'A':'a'},columns={'B':'b'}) #通过字典修改某一项 67 # map函数的参数可以使自己定义的函数 68 69 70 #dataframe的merge操作 71 pd.merge(df1,df2,on='name',how='inner') # on表示根据哪列的name来作为判断依据,默认为None,how的参数中比如写left,那么就根据左侧的df;爱显示数据,若右边一个没有的则补全为nan,outer是right和left的结合,将所有的都输出 72 df1 = DataFrame({'key':['A','B','C'],'data_set_1':[1,2,3]}) 73 df2 = DataFrame({'key':['X','Y','Z'],'data_set_2':[4,5,6]}) 74 pd.merge(df1,df2) # 这时的结果返回为空,因为merge是对其中key值相同的进行操作 75 # 当相同name的columns(例如此例的key)时,他中的值相同,那么可以进行merge 76 77 78 #concatenate和combine 79 80 #~~concatenate: 81 82 #1、array 83 arr1 = np.arange(9).reshape(3,3) 84 arr2 = np.arange(9).reshape(3,3) 85 np.concatenate([arr1,arr2]) #通过列表放在一起
  ''' 86 output:array([[0, 1, 2], 87 [3, 4, 5], 88 [6, 7, 8], 89 [0, 1, 2], 90 [3, 4, 5], 91 [6, 7, 8]]) 92 注:其中concatenate的参数包括axis,可以决定如何连接
  '''
93 94 #2、Series 95 s1 = Series([1,2,3],index=['X','Y','Z']) 96 s2 = Series([4,5],index=['A','B']) 97 pd.concat([s1,s2]) #同样有axis参数,为0则在下面连接,为1则横向 98 99 #3、dataframe 100 df1 = DataFrame(np.random.randn(4,3),columns=['X','Y','Z']) 101 df2 = DataFrame(np.random.randn(3,3),columns=['X','Y','A']) 102 pd.concat([df1,df2])
  '''
103 Out: 104 A X Y Z 105 0 NaN -0.060523 0.879124 1.673622 106 1 NaN 0.734367 0.708085 -0.133981 107 2 NaN 0.461922 -2.186110 -4.473558 108 3 NaN 1.553153 -2.256533 -0.381862 109 0 1.304371 -0.275638 1.362799 NaN 110 1 -0.357986 -0.273505 0.430566 NaN 111 2 1.406862 1.453295 -0.681261 NaN
  '''
112 113 #~~combine: 114 115 #1、Series: 116 s1 = Series([2,np.nan,4,np.nan],index=['A','B','C','D']) 117 s1 = Series([1,2,3,4],index=['A','B','C','D']) 118 s1.combine_first(s2) #把s1中没有的填充上从s2 119 120 #2、DataFrame: 121 #和series几乎一样

猜你喜欢

转载自www.cnblogs.com/yudanqu/p/8995519.html