不管是業(yè)務(wù)數(shù)據(jù)分析 ,還是數(shù)據(jù)建模。數(shù)據(jù)處理都是及其重要的一個(gè)步驟,它對(duì)于最終的結(jié)果來(lái)說(shuō),至關(guān)重要。
今天,就為大家總結(jié)一下 “Pandas數(shù)據(jù)處理” 幾個(gè)方面重要的知識(shí),拿來(lái)即用,隨查隨查。
- 導(dǎo)?數(shù)據(jù)
- 導(dǎo)出數(shù)據(jù)
- 查看數(shù)據(jù)
- 數(shù)據(jù)選取
- 數(shù)據(jù)處理
- 數(shù)據(jù)分組和排序
- 數(shù)據(jù)合并
1
2
|
# 在使用之前,需要導(dǎo)入pandas庫(kù) import pandas as pd |
導(dǎo)?數(shù)據(jù)
這里我為大家總結(jié)7個(gè)常見(jiàn)用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
pd.DataFrame() # 自己創(chuàng)建數(shù)據(jù)框,用于練習(xí) pd.read_csv(filename) # 從CSV?件導(dǎo)?數(shù)據(jù) pd.read_table(filename) # 從限定分隔符的?本?件導(dǎo)?數(shù)據(jù) pd.read_excel(filename) # 從Excel?件導(dǎo)?數(shù)據(jù) pd.read_sql(query,connection_object) # 從SQL表/庫(kù)導(dǎo)?數(shù)據(jù) pd.read_json(json_string) # 從JSON格式的字符串導(dǎo)?數(shù)據(jù) pd.read_html(url) # 解析URL、字符串或者HTML?件,抽取其中的tables表格 |
導(dǎo)出數(shù)據(jù)
這里為大家總結(jié)5個(gè)常見(jiàn)用法。
1
2
3
4
5
6
7
8
9
10
|
df.to_csv(filename) #導(dǎo)出數(shù)據(jù)到CSV?件 df.to_excel(filename) #導(dǎo)出數(shù)據(jù)到Excel?件 df.to_sql(table_name,connection_object) #導(dǎo)出數(shù)據(jù)到SQL表 df.to_json(filename) #以Json格式導(dǎo)出數(shù)據(jù)到?本?件 writer = pd.ExcelWriter( 'test.xlsx' ,index = False ) df1.to_excel(writer,sheet_name = '單位' )和writer.save(),將多個(gè)數(shù)據(jù)幀寫(xiě)?同?個(gè)?作簿的多個(gè)sheet(?作表) |
查看數(shù)據(jù)
這里為大家總結(jié)11個(gè)常見(jiàn)用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
df.head(n) # 查看DataFrame對(duì)象的前n? df.tail(n) # 查看DataFrame對(duì)象的最后n? df.shape() # 查看?數(shù)和列數(shù) df.info() # 查看索引、數(shù)據(jù)類(lèi)型和內(nèi)存信息 df.columns() # 查看字段(??)名稱(chēng) df.describe() # 查看數(shù)值型列的匯總統(tǒng)計(jì) s.value_counts(dropna = False ) # 查看Series對(duì)象的唯?值和計(jì)數(shù) df. apply (pd.Series.value_counts) # 查看DataFrame對(duì)象中每?列的唯?值和計(jì)數(shù) df.isnull(). any () # 查看是否有缺失值 df[df[column_name].duplicated()] # 查看column_name字段數(shù)據(jù)重復(fù)的數(shù)據(jù)信息 df[df[column_name].duplicated()].count() # 查看column_name字段數(shù)據(jù)重復(fù)的個(gè)數(shù) |
數(shù)據(jù)選取
這里為大家總結(jié)10個(gè)常見(jiàn)用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
df[col] # 根據(jù)列名,并以Series的形式返回列 df[[col1,col2]] # 以DataFrame形式返回多列 s.iloc[ 0 ] # 按位置選取數(shù)據(jù) s.loc[ 'index_one' ] # 按索引選取數(shù)據(jù) df.iloc[ 0 ,:] # 返回第?? df.iloc[ 0 , 0 ] # 返回第?列的第?個(gè)元素 df.loc[ 0 ,:] # 返回第??(索引為默認(rèn)的數(shù)字時(shí),?法同df.iloc),但需要注意的是loc是按索引,iloc參數(shù)只接受數(shù)字參數(shù) df.ix[[: 5 ],[ "col1" , "col2" ]] # 返回字段為col1和col2的前5條數(shù)據(jù),可以理解為loc和 iloc的結(jié)合體。 df.at[ 5 , "col1" ] # 選擇索引名稱(chēng)為5,字段名稱(chēng)為col1的數(shù)據(jù) df.iat[ 5 , 0 ] # 選擇索引排序?yàn)?,字段排序?yàn)?的數(shù)據(jù) |
數(shù)據(jù)處理
這里為大家總結(jié)16個(gè)常見(jiàn)用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
df.columns = [ 'a' , 'b' , 'c' ] # 重命名列名(需要將所有列名列出,否則會(huì)報(bào)錯(cuò)) pd.isnull() # 檢查DataFrame對(duì)象中的空值,并返回?個(gè)Boolean數(shù)組 pd.notnull() # 檢查DataFrame對(duì)象中的?空值,并返回?個(gè)Boolean數(shù)組 df.dropna() # 刪除所有包含空值的? df.dropna(axis = 1 ) # 刪除所有包含空值的列 df.dropna(axis = 1 ,thresh = n) # 刪除所有?于n個(gè)?空值的? df.fillna(value = x) # ?x替換DataFrame對(duì)象中所有的空值,?持 df[column_name].fillna(x) s.astype( float ) # 將Series中的數(shù)據(jù)類(lèi)型更改為float類(lèi)型 s.replace( 1 , 'one' ) # ?‘one'代替所有等于1的值 s.replace([ 1 , 3 ],[ 'one' , 'three' ]) # ?'one'代替1,?'three'代替3 df.rename(columns = lambdax:x + 1 ) # 批量更改列名 df.rename(columns = { 'old_name' : 'new_ name' }) # 選擇性更改列名 df.set_index( 'column_one' ) # 將某個(gè)字段設(shè)為索引,可接受列表參數(shù),即設(shè)置多個(gè)索引 df.reset_index( "col1" ) # 將索引設(shè)置為col1字段,并將索引新設(shè)置為0,1,2... df.rename(index = lambdax:x + 1 ) # 批量重命名索引 |
數(shù)據(jù)分組、排序、透視
這里為大家總結(jié)13個(gè)常見(jiàn)用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
df.sort_index().loc[: 5 ] # 對(duì)前5條數(shù)據(jù)進(jìn)?索引排序 df.sort_values(col1) # 按照列col1排序數(shù)據(jù),默認(rèn)升序排列 df.sort_values(col2,ascending = False ) # 按照列col1降序排列數(shù)據(jù) df.sort_values([col1,col2],ascending = [ True , False ]) # 先按列col1升序排列,后按col2降序排列數(shù)據(jù) df.groupby(col) # 返回?個(gè)按列col進(jìn)?分組的Groupby對(duì)象 df.groupby([col1,col2]) # 返回?個(gè)按多列進(jìn)?分組的Groupby對(duì)象 df.groupby(col1)[col2].agg(mean) # 返回按列col1進(jìn)?分組后,列col2的均值,agg可以接受列表參數(shù),agg([len,np.mean]) df.pivot_table(index = col1,values = [col2,col3],aggfunc = {col2: max ,col3:[ma, min ]}) # 創(chuàng)建?個(gè)按列col1進(jìn)?分組,計(jì)算col2的最?值和col3的最?值、最?值的數(shù)據(jù)透視表 df.groupby(col1).agg(np.mean) # 返回按列col1分組的所有列的均值,?持 df.groupby(col1).col2.agg([ 'min' , 'max' ]) data. apply (np.mean) # 對(duì)DataFrame中的每?列應(yīng)?函數(shù)np.mean data. apply (np. max ,axis = 1 ) # 對(duì)DataFrame中的每??應(yīng)?函數(shù)np.max df.groupby(col1).col2.transform( "sum" ) # 通常與groupby連?,避免索引更改 |
數(shù)據(jù)合并
這里為大家總結(jié)5個(gè)常見(jiàn)用法。
1
2
3
4
5
6
7
8
9
|
df1.append(df2) # 將df2中的?添加到df1的尾部 df.concat([df1,df2],axis = 1 ,join = 'inner' ) # 將df2中的列添加到df1的尾部,值為空的對(duì)應(yīng)?與對(duì)應(yīng)列都不要 df1.join(df2.set_index(col1),on = col1,how = 'inner' ) # 對(duì)df1的列和df2的列執(zhí)?SQL形式的join,默認(rèn)按照索引來(lái)進(jìn)?合并,如果df1和df2有共同字段時(shí),會(huì)報(bào)錯(cuò),可通過(guò)設(shè)置lsuffix,rsuffix來(lái)進(jìn)?解決,如果需要按照共同列進(jìn)?合并,就要?到set_index(col1) pd.merge(df1,df2,on = 'col1' ,how = 'outer' ) # 對(duì)df1和df2合并,按照col1,?式為outer pd.merge(df1,df2,left_index = True ,right_index = True ,how = 'outer' ) #與 df1.join(df2, how='outer')效果相同 |
以上就是python數(shù)據(jù)處理67個(gè)pandas函數(shù)總結(jié)看完就用的詳細(xì)內(nèi)容,更多關(guān)于python數(shù)據(jù)處理6pandas函數(shù)的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://huang-tong-xue.blog.csdn.net/article/details/115598697