pandas对Numpy进行了封装,简化了操作。其数据结构主要是DataFrame(类似于多维数组)和Series(类似于一维数组)。
pip install pandas
import pandas as pd
>>> pd.Series([10,20,30,40,50]) #列表形式,不指定index,默认从0开始整数序列 0 10 1 20 2 30 3 40 4 50 dtype: int64 >>> pd.Series([10,20,30,40,50],index=list(‘abcde‘)) #列表形式,指定index a 10 b 20 c 30 d 40 e 50 dtype: int64 >>> pd.Series([10,20,30,40,50],index=list(‘abcde‘),dtype=float) #列表形式,指定index和dtype,数据强转成float类型 a 10.0 b 20.0 c 30.0 d 40.0 e 50.0 dtype: float64 >>> pd.Series({‘a‘:100,‘b‘:200,‘c‘:300}) #字典形式 a 100 b 200 c 300 dtype: int64 >>> pd.Series(np.arange(10,15)) #ndarray形式 0 10 1 11 2 12 3 13 4 14 dtype: int64 >>> pd.Series(6,index=list(‘abc‘)) #标量形式 a 6 b 6 c 6 dtype: int64
>>> data = pd.Series(np.arange(10,15),index=list(‘abcde‘)) >>> data a 10 b 11 c 12 d 13 e 14 dtype: int64 >>> ‘a‘ in data True >>> ‘f‘ in data False >>> data.get(‘m‘,100) 100
>>> data = pd.Series(np.arange(10,15),index=list(‘abcde‘)) >>> data.index Index([‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘], dtype=‘object‘) >>> data.values array([10, 11, 12, 13, 14])
>>> data a 10 b 11 c 12 d 13 e 14 dtype: int64 >>> data.sort_values(ascending = False) #降序排序 e 14 d 13 c 12 b 11 a 10 dtype: int64 >>> data = pd.Series([100,200,np.nan,200,np.nan,400],list(‘abcdef‘)) #创建含有缺失值的对象 >>> data a 100.0 b 200.0 c NaN d 200.0 e NaN f 400.0 dtype: float64 >>> data.isnull() #判空 a False b False c True d False e True f False dtype: bool >>> data.notnull() #判非空 a True b True c False d True e False f True dtype: bool >>> data.dropna() #删除缺失值 a 100.0 b 200.0 d 200.0 f 400.0 dtype: float64 >>> data.fillna(data.mean()) #设置默认值为均值 a 100.0 b 200.0 c 225.0 d 200.0 e 225.0 f 400.0 dtype: float64 >>> data.drop_duplicates() #去重 a 100.0 b 200.0 c NaN f 400.0 dtype: float64 >>> data.value_counts() #统计频率 200.0 2 100.0 1 400.0 1 dtype: int64 >>> data.describe() #对数据进行基本统计,统计时自动去掉了缺失值 count 4.000000 mean 225.000000 std 125.830574 min 100.000000 25% 175.000000 50% 200.000000 75% 250.000000 max 400.000000 dtype: float64
>>> data = pd.Series(np.arange(10,15),index=list(‘abcde‘)) #索引为非整数序列 >>> data a 10 b 11 c 12 d 13 e 14 dtype: int64 >>> data[‘a‘] #根据键进行索引 10 >>> data.loc[‘a‘] #严格按照键进行索引 10 >>> data[‘a‘:‘d‘] #根据键进行切片,不同于普通下标切片,结果包含最后一个 a 10 b 11 c 12 d 13 dtype: int64 >>> data.loc[‘a‘:‘d‘] #严格按照键来切片 a 10 b 11 c 12 d 13 dtype: int64 >>> data[3] #按下标进行索引 13 >>> data.iloc[3] #严格按下标进行索引 13 >>> data[2:4] #按下标进行切片,结果不包含‘:‘右边这个下标 c 12 d 13 dtype: int64 >>> data.iloc[2:4] #严格按下标进行切片 c 12 d 13 dtype: int64 >>> data2 = pd.Series(np.arange(10,15),index=range(2,7)) #索引为整数序列 >>> data2 2 10 3 11 4 12 5 13 6 14 dtype: int64 >>> data2[2] #整数索引,键的优先级更高 10 >>> data2.iloc[2] #严格按照下标索引,结果是第三个值12 12 >>> data2[2:4] #整数切片,只会按照下标 4 12 5 13 dtype: int64 >>> data2.loc[2:4] #指定按照键来切片,结果获得键为2、3、4的值 2 10 3 11 4 12 dtype: int64 >>> data2[[2,4]] #传入数组,分别获取键为2和4的索引值 2 10 4 12 dtype: int64 >>> data2[(data2>11) & (data2<14)] #过滤11-14之间的数 4 12 5 13 dtype: int64
>>> pd.DataFrame(np.arange(15).reshape(3,5)) #通过二维数组创建,自动添加行索引和列索引 0 1 2 3 4 0 0 1 2 3 4 1 5 6 7 8 9 2 10 11 12 13 14 >>> pd.DataFrame(np.arange(15).reshape(3,5),index=list(‘abc‘)) #通过index参数指定行索引 0 1 2 3 4 a 0 1 2 3 4 b 5 6 7 8 9 c 10 11 12 13 14 >>>pd.DataFrame({"first":np.arange(1,5),"second":np.arange(5,9)}) #字典值为一维数组 first second 0 1 5 1 2 6 2 3 7 3 4 8 >>> pd.DataFrame({"first":[1,2,3],"second":[4,5,6]},index=list(‘abc‘)) #字典值为列表 first second a 1 4 b 2 5 c 3 6 >>> pd.DataFrame({"first":pd.Series([1,2,3],index=list(‘abc‘)),"second":pd.Series([6,7,8,9],index=list(‘bcde‘))}) #值为Series对象 first second a 1.0 NaN b 2.0 6.0 c 3.0 7.0 d NaN 8.0 e NaN 9.0
>>> pd.read_excel(‘ship_ticket.xlsx‘,names=[‘name‘,‘age‘,‘class‘,‘fares‘],dtype={‘fares‘:float}) #names参数可以添加或重设列名 name age class fares 0 PFp 22 1 121.0 1 UtG 45 1 143.0 2 NWT 49 1 171.0 3 pHK 45 2 71.0 4 1AD 24 3 42.0 .. ... ... ... ... 195 BO6 29 1 165.0 196 Xcu 45 1 193.0 197 LPB 35 3 26.0 198 0kx 40 2 89.0 199 ulK 41 2 87.0 [200 rows x 4 columns]
>>> data first second a 1 4 b 2 5 c 3 6 >>> data.index #行索引 Index([‘a‘, ‘b‘, ‘c‘], dtype=‘object‘) >>> data.columns #列索引 Index([‘first‘, ‘second‘], dtype=‘object‘) >>> data.values #以二维数组的形式显示所有值 array([[1, 4], [2, 5], [3, 6]]) >>> data.shape #行和列大小 (3, 2) >>> data.T #转置 a b c first 1 2 3 second 4 5 6
>>> data = pd.read_excel(‘ship_ticket.xlsx‘) >>> data name age class fares 0 PFp 22 1 121 1 UtG 45 1 143 2 NWT 49 1 171 3 pHK 45 2 71 4 1AD 24 3 42 .. ... ... ... ... 195 BO6 29 1 165 196 Xcu 45 1 193 197 LPB 35 3 26 198 0kx 40 2 89 199 ulK 41 2 87 [200 rows x 4 columns] >>> data.head() #显示前5行 name age class fares 0 PFp 22 1 121 1 UtG 45 1 143 2 NWT 49 1 171 3 pHK 45 2 71 4 1AD 24 3 42 >>> data.tail(3) #显示后3行 name age class fares 197 LPB 35 3 26 198 0kx 40 2 89 199 ulK 41 2 87 >>> data.count() #统计每行的非空值数 name 200 age 200 class 200 fares 200 dtype: int64 >>> data.describe() #统计每一列的基本信息 age class fares count 200.000000 200.000000 200.000000 mean 34.580000 2.025000 84.735000 std 8.599614 0.847328 53.714159 min 20.000000 1.000000 20.000000 25% 27.000000 1.000000 40.000000 50% 35.000000 2.000000 71.000000 75% 42.000000 3.000000 124.250000 max 49.000000 3.000000 199.000000 >>> data.rename(columns={‘name‘:‘new_name‘}) #修改列名‘name’为‘new_name’ new_name age class fares 0 PFp 22 1 121 1 UtG 45 1 143 2 NWT 49 1 171 3 pHK 45 2 71 4 1AD 24 3 42 .. ... ... ... ... 195 BO6 29 1 165 196 Xcu 45 1 193 197 LPB 35 3 26 198 0kx 40 2 89 199 ulK 41 2 87 [200 rows x 4 columns] >>> data.drop([‘age‘,‘fares‘],axis=1) #删除‘age‘和‘fares‘列 name class 0 PFp 1 1 UtG 1 2 NWT 1 3 pHK 2 4 1AD 3 .. ... ... 195 BO6 1 196 Xcu 1 197 LPB 3 198 0kx 2 199 ulK 2 [200 rows x 2 columns] >>> data.sort_values(‘fares‘,ascending=False).head(5) #按fares降序排列 name age class fares 194 BTs 41 1 199 139 gC0 31 1 196 6 j8B 20 1 194 196 Xcu 45 1 193 36 KGj 39 1 193 >>> data.sort_values(‘fares‘,ascending=False).head(5).reset_index(drop=True) #排序后重设index name age class fares 0 BTs 41 1 199 1 gC0 31 1 196 2 j8B 20 1 194 3 Xcu 45 1 193 4 KGj 39 1 193 >>> data.pivot_table(index=‘class‘,values=‘fares‘,aggfunc=‘mean‘) #数据透视表,求各class的平均fares fares class 1 149.492754 2 72.982456 3 33.405405 >>> data.drop_duplicates(subset=[‘age‘,‘fares‘]) #按照age和fares去重,原先200行,现在196行 name age class fares 0 PFp 22 1 121 1 UtG 45 1 143 2 NWT 49 1 171 3 pHK 45 2 71 4 1AD 24 3 42 .. ... ... ... ... 195 BO6 29 1 165 196 Xcu 45 1 193 197 LPB 35 3 26 198 0kx 40 2 89 199 ulK 41 2 87 [196 rows x 4 columns] >>> data = pd.DataFrame({‘first‘:pd.Series([10,20,None,40],index=list(‘abcd‘)),‘second‘:pd.Series([100,None,300,None,500],index=list(‘bcdef‘))}) >>> data first second a 10.0 NaN b 20.0 100.0 c NaN NaN d 40.0 300.0 e NaN NaN f NaN 500.0 >>> data.dropna(how=‘all‘) #删除空行 first second a 10.0 NaN b 20.0 100.0 d 40.0 300.0 f NaN 500.0 >>> data.dropna(subset=[‘second‘]) #如果’sencond‘列有缺失值,就删除该行 first second b 20.0 100.0 d 40.0 300.0 f NaN 500.0 >>> data.fillna(0) #给缺失值填充为0 first second a 10.0 0.0 b 20.0 100.0 c 0.0 0.0 d 40.0 300.0 e 0.0 0.0 f 0.0 500.0
>>> data = pd.DataFrame({"first":[1,2,3],"second":[4,5,6]},index=list(‘abc‘)) >>> data[‘first‘] #获取‘first‘列 a 1 b 2 c 3 Name: first, dtype: int64 >>> data[‘third‘] = [10,20,30] #增加third列 >>> data first second third a 1 4 10 b 2 5 20 c 3 6 30 >>> del data[‘first‘] #删除‘first‘列 >>> data second third a 4 10 b 5 20 c 6 30
>>> data first second a 1 4 b 2 5 c 3 6 >>> data[‘a‘:‘c‘] #按键切片 first second a 1 4 b 2 5 c 3 6 >>> data[0:2] #按下标切片 first second a 1 4 b 2 5 >>> data.loc[‘a‘] #取键索引为‘a‘行数据 first 1 second 4 Name: a, dtype: int64 >>> data.loc[[‘a‘,‘c‘]] #取键索引为‘a‘和‘c‘行数据 first second a 1 4 c 3 6 >>> data.iloc[1] #取下标为1的行数据,即第2行 first 2 second 5 Name: b, dtype: int64 >>> data.iloc[[1,2]] #取下标为2和3的行数据,即第3-4行 first second b 2 5 c 3 6 >>> data.loc[‘b‘,‘first‘] #定位键索引‘b‘行‘first‘列数据 2 >>> data.loc[[‘b‘,‘c‘],‘first‘] #‘c‘和‘c‘行‘first’列数据 b 2 c 3 Name: first, dtype: int64 >>> data.loc[‘b‘:‘c‘,‘first‘] #行使用切片 b 2 c 3 Name: first, dtype: int64 >>> data.loc[‘b‘:‘c‘,‘first‘:‘second‘] #行和列均使用切片 first second b 2 5 c 3 6 >>> data.loc[‘b‘:‘c‘,[‘first‘,‘second‘]] #获取多行多列数据 first second b 2 5 c 3 6
>>> data[data[‘second‘]%2==0] #获取‘second‘列值为偶数的行数据 first second a 1 4 c 3 6 >>> data[data[‘first‘]<3] #获取‘first‘列值小于3的行数据 first second a 1 4 b 2 5
原文:https://www.cnblogs.com/eliwang/p/14779283.html