因爲series對象很簡單,跟數組相似,但多了一些額外的功能,偷個懶,用思惟導圖表示數組
DataFrame將Series的使用場景由一維擴展到多維,數據結構跟Excel工做表極爲類似,說白了就是矩陣數據結構
DataFrame對象的構造分三部分:數據data,行標籤index和列標籤columns,下面給出三種構造方法函數
data = {'color':['blue','green','yellow','red','white'], 'object':['ball','pen','pencil','paper','mug'], 'price':[1.2,1.0,0.6,0.9,1.7]} #構造DataFrame方法1 frame1 = pd.DataFrame(data) print(frame1) #構造DataFrame方法2 frame2 = pd.DataFrame(data,columns=['object','price']) print(frame2) #構造DataFrame方法3 frame3 = pd.DataFrame(data,columns=['object','price'],index=['a','b','c','d','e']) print(frame3)
上面代碼中的data能夠爲字典,ndarray和matrix對象spa
(1)獲取行標(index)--->frame.index.net
(2)獲取列標(columns)--->frame.columnscode
(3)獲取數據結構中的全部元素 --->frame.values 對象
(4)獲取每一列的元素 --->frame['price']或frame.priceblog
(5)獲取dataframe中的行信息,可使用ix方法的索引和數組方式或frame的切片方法索引
frame.ix[2] --->獲取第3行的信息ip
frame.ix[[2,4]] --->獲取第3行和第5行的信息
frame[1:3] --->獲取索引爲1和2的行信息
(6)獲取指定cell元素 --->frame['price'][3]
(7)根據元素值進行篩選,好比:--->frame[frame < 12]
經過選取元素一樣的邏輯就能增長和修改元素
(1)修改指定元素的值 --->frame['price'][3] = 8.6
(2)增長新列new,指定每行的值都爲12 --->frame['new'] = 12
(3)更新指定列的內容 --->frame['new'] = [1,2,3,4,5]
一樣也可使用Series對象爲列賦值
array = np.arange(5) series = pd.Series(array,index=['a','b','c','d','e']) print(series) frame3['new'] = series print(frame3)
輸出:
刪除指定行和指定列都使用drop函數
例:
#刪除標籤爲'a'和'b'的行 frame4 = frame3.drop(['a','b'],axis=0,inplace=False) print('刪除指定行:\n',frame4) #刪除標籤爲'price'的列 frame5 = frame3.drop(['price'],axis=1,inplace=False) print('刪除指定列:\n',frame5)
輸出:
和:sum():
均值:mean()
計算多個統計量:describe()
相關性:corr()
協方差:cov()
array = np.array([[1,4,3,6],[4,5,6,1],[3,3,1,5],[4,1,6,4]]) index = ['red','blue','yellow','white'] columns = ['ball','pen','pencil','paper'] frame = pd.DataFrame(array,index=index,columns=columns) print(frame)
print(frame.corr())
print(frame.cov())
a b c 0 0 2 4 1 6 8 10 2 12 14 16 3 18 20 22 4 24 26 28 5 30 32 34 6 36 38 40 7 42 44 46 8 48 50 52 9 54 56 58
df[df['a']>30] # 若是想篩選a列的取值大於30的記錄,可是之顯示知足條件的b,c列的值能夠這麼寫 df[['b','c']][df['a']>30] # 使用isin函數根據特定值篩選記錄。篩選a值等於30或者54的記錄 df[df.a.isin([30, 54])]
可使用&(並)與| (或)操做符或者特定的函數實現多條件篩選
# 使用&篩選a列的取值大於30,b列的取值大於40的記錄 df[(df['a'] > 30) & (df['b'] > 40)]
df[行索引,列索引]或df[[列名1,列名2]]
#使用切片操做選擇特定的行 df[1:4] #傳入列名選擇特定的列 df[['a','c']]
當每列已有column name時,用 df [ ‘a’ ] 就能選取出一整列數據。若是你知道column names 和index,且二者都很好輸入,能夠選擇 .loc同時進行行列選擇。
In [28]: df.loc[0,'c'] Out[28]: 4 In [29]: df.loc[1:4,['a','c']] Out[29]: a c 1 6 10 2 12 16 3 18 22 4 24 28 In [30]: df.loc[[1,3,5],['a','c']] Out[30]: a c 1 6 10 3 18 22 5 30 34
若是column name太長,輸入不方便,或者index是一列時間序列,更很差輸入,那就能夠選擇 .iloc了,該方法接受列名的index,iloc 使得咱們能夠對column使用slice(切片)的方法對數據進行選取。這邊的 i 我以爲表明index,比較好記點。
In [35]: df.iloc[0,2] Out[35]: 4 In [34]: df.iloc[1:4,[0,2]] Out[34]: a c 1 6 10 2 12 16 3 18 22 In [36]: df.iloc[[1,3,5],[0,2]] Out[36]: a c 1 6 10 3 18 22 5 30 34 In [38]: df.iloc[[1,3,5],0:2] Out[38]: a b 1 6 8 3 18 20 5 30 32
ix的功能更增強大,參數既能夠是索引,也能夠是名稱,至關於,loc和iloc的合體。須要注意的是在使用的時候須要統一,在行選擇時同時出現索引和名稱, 一樣在同行選擇時同時出現索引和名稱。
df.ix[1:3,['a','b']] Out[41]: a b 1 6 8 2 12 14 3 18 20 In [42]: df.ix[[1,3,5],['a','b']] Out[42]: a b 1 6 8 3 18 20 5 30 32 In [45]: df.ix[[1,3,5],[0,2]] Out[45]: a c 1 6 10 3 18 22 5 30 34
根據指定行index及列label,快速定位DataFrame的元素,選擇列時僅支持列名。
In [46]: df.at[3,'a'] Out[46]: 18
與at的功能相同,只使用索引參數
In [49]: df.iat[3,0] Out[49]: 18
Supplier Name,Invoice Number,Part Number,Cost,Purchase Date Supplier X,001-1001,2341,$500.00 ,1/20/14 Supplier X,001-1001,2341,$500.00 ,1/20/14 Supplier X,001-1001,5467,$750.00 ,1/20/14 Supplier X,001-1001,5467,$750.00 ,1/20/14 Supplier Y,50-9501,7009,$250.00 ,1/30/14 Supplier Y,50-9501,7009,$250.00 ,1/30/14 Supplier Y,50-9505,6650,$125.00 ,2002/3/14 Supplier Y,50-9505,6650,$125.00 ,2002/3/14 Supplier Z,920-4803,3321,$615.00 ,2002/3/14 Supplier Z,920-4804,3321,$615.00 ,2002/10/14 Supplier Z,920-4805,3321,$615.00 ,2/17/14 Supplier Z,920-4806,3321,$615.00 ,2/24/14
關於read_csv函數中的參數說明參考博客:https://blog.csdn.net/liuweiyuxiang/article/details/78471036
import pandas as pd # 讀寫csv文件 df = pd.read_csv("supplier_data.csv") df.to_csv("supplier_data_write.csv",index=None)
#Supplier Nmae列中姓名包含'Z',或者Cost列中的值大於600 print(df[df["Supplier Name"].str.contains('Z')]) print(df[df['Cost'].str.strip('$').astype(float) > 600]) print(df.loc[(df["Supplier Name"].str.contains('Z'))|(df['Cost'].str.strip('$').astype(float) > 600.0),:]) #行中的值屬於某個集合 li = [2341,6650] print(df[df['Part Number'].isin(li)]) print(df.loc[df['Part Number'].astype(int).isin(li),:]) #行中的值匹配某個模式 print(df[df['Invoice Number'].str.startswith("001-")])
#選取特定的列 #列索引值,打印1,3列 print(df.iloc[:,1:4:2]) #列標題打印 print(df.loc[:,["Invoice Number", "Part Number"]]) #選取連續的行 print(df.loc[1:4,:])