激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python數(shù)據(jù)處理67個pandas函數(shù)總結(jié)看完就用

python數(shù)據(jù)處理67個pandas函數(shù)總結(jié)看完就用

2022-02-26 00:12數(shù)據(jù)分析與統(tǒng)計學(xué)之美 Python

這篇文章主要介紹了python數(shù)據(jù)處理67個pandas函數(shù)的梳理總結(jié),看完就可以去用了,有需要的朋友可以借鑒參考下,希望能夠有所幫助

不管是業(yè)務(wù)數(shù)據(jù)分析 ,還是數(shù)據(jù)建模。數(shù)據(jù)處理都是及其重要的一個步驟,它對于最終的結(jié)果來說,至關(guān)重要。

今天,就為大家總結(jié)一下 “Pandas數(shù)據(jù)處理” 幾個方面重要的知識,拿來即用,隨查隨查。

  • 導(dǎo)?數(shù)據(jù)
  • 導(dǎo)出數(shù)據(jù)
  • 查看數(shù)據(jù)
  • 數(shù)據(jù)選取
  • 數(shù)據(jù)處理
  • 數(shù)據(jù)分組和排序
  • 數(shù)據(jù)合并
?
1
2
# 在使用之前,需要導(dǎo)入pandas庫
import pandas as pd

導(dǎo)?數(shù)據(jù)

這里我為大家總結(jié)7個常見用法。

?
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表/庫導(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個常見用法。

?
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(),將多個數(shù)據(jù)幀寫?同?個?作簿的多個sheet(?作表)

查看數(shù)據(jù)

這里為大家總結(jié)11個常見用法。

?
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對象的前n?
 
df.tail(n) # 查看DataFrame對象的最后n?
 
df.shape() # 查看?數(shù)和列數(shù)
 
df.info() # 查看索引、數(shù)據(jù)類型和內(nèi)存信息
 
df.columns() # 查看字段(??)名稱
 
df.describe() # 查看數(shù)值型列的匯總統(tǒng)計
 
s.value_counts(dropna=False) # 查看Series對象的唯?值和計數(shù)
 
df.apply(pd.Series.value_counts) # 查看DataFrame對象中每?列的唯?值和計數(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ù)的個數(shù)

數(shù)據(jù)選取

這里為大家總結(jié)10個常見用法。

?
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] # 返回第?列的第?個元素
 
df.loc[0,:] # 返回第??(索引為默認的數(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"] # 選擇索引名稱為5,字段名稱為col1的數(shù)據(jù)
 
df.iat[5,0] # 選擇索引排序為5,字段排序為0的數(shù)據(jù)

數(shù)據(jù)處理

這里為大家總結(jié)16個常見用法。

?
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'] # 重命名列名(需要將所有列名列出,否則會報錯)
 
pd.isnull() # 檢查DataFrame對象中的空值,并返回?個Boolean數(shù)組
 
pd.notnull() # 檢查DataFrame對象中的?空值,并返回?個Boolean數(shù)組
 
df.dropna() # 刪除所有包含空值的?
 
df.dropna(axis=1) # 刪除所有包含空值的列
 
df.dropna(axis=1,thresh=n) # 刪除所有?于n個?空值的?
 
df.fillna(value=x) # ?x替換DataFrame對象中所有的空值,?持
 
df[column_name].fillna(x)
 
s.astype(float) # 將Series中的數(shù)據(jù)類型更改為float類型
 
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') # 將某個字段設(shè)為索引,可接受列表參數(shù),即設(shè)置多個索引
 
df.reset_index("col1") # 將索引設(shè)置為col1字段,并將索引新設(shè)置為0,1,2...
 
df.rename(index=lambdax:x+1) # 批量重命名索引

數(shù)據(jù)分組、排序、透視

這里為大家總結(jié)13個常見用法。

?
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] # 對前5條數(shù)據(jù)進?索引排序
 
df.sort_values(col1) # 按照列col1排序數(shù)據(jù),默認升序排列
 
df.sort_values(col2,ascending=False) # 按照列col1降序排列數(shù)據(jù)
 
df.sort_values([col1,col2],ascending=[True,False]) # 先按列col1升序排列,后按col2降序排列數(shù)據(jù)
 
df.groupby(col) # 返回?個按列col進?分組的Groupby對象
 
df.groupby([col1,col2]) # 返回?個按多列進?分組的Groupby對象
 
df.groupby(col1)[col2].agg(mean) # 返回按列col1進?分組后,列col2的均值,agg可以接受列表參數(shù),agg([len,np.mean])
 
df.pivot_table(index=col1,values=[col2,col3],aggfunc={col2:max,col3:[ma,min]}) # 創(chuàng)建?個按列col1進?分組,計算col2的最?值和col3的最?值、最?值的數(shù)據(jù)透視表
 
df.groupby(col1).agg(np.mean) # 返回按列col1分組的所有列的均值,?持
 
df.groupby(col1).col2.agg(['min','max'])
 
data.apply(np.mean) # 對DataFrame中的每?列應(yīng)?函數(shù)np.mean
 
data.apply(np.max,axis=1) # 對DataFrame中的每??應(yīng)?函數(shù)np.max
 
df.groupby(col1).col2.transform("sum") # 通常與groupby連?,避免索引更改

數(shù)據(jù)合并

這里為大家總結(jié)5個常見用法。

?
1
2
3
4
5
6
7
8
9
df1.append(df2) # 將df2中的?添加到df1的尾部
 
df.concat([df1,df2],axis=1,join='inner') # 將df2中的列添加到df1的尾部,值為空的對應(yīng)?與對應(yīng)列都不要
 
df1.join(df2.set_index(col1),on=col1,how='inner') # 對df1的列和df2的列執(zhí)?SQL形式的join,默認按照索引來進?合并,如果df1和df2有共同字段時,會報錯,可通過設(shè)置lsuffix,rsuffix來進?解決,如果需要按照共同列進?合并,就要?到set_index(col1)
 
pd.merge(df1,df2,on='col1',how='outer') # 對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個pandas函數(shù)總結(jié)看完就用的詳細內(nèi)容,更多關(guān)于python數(shù)據(jù)處理6pandas函數(shù)的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!

原文鏈接:https://huang-tong-xue.blog.csdn.net/article/details/115598697

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91九色视频观看 | 中文字幕激情视频 | 久久久久久久久久亚洲精品 | 黄色特级毛片 | 欧美一级做一级爱a做片性 毛片电影网址 | 午夜生活理论片 | 国产精品视频在 | 欧美一区二区三区久久精品视 | 久久成年网站 | 宅男噜噜噜66国产免费观看 | 国产九色视频在线观看 | 久久人体 | 超久久| 国产乱淫av | 欧美精品一区二区性色 | 久久国产精品影视 | 精品国产91久久久久 | 中国av免费在线观看 | 久久91亚洲人成电影网站 | 欧美黄一区| 美女污污视频在线观看 | 欧美人xx | 日本在线视频二区 | 中文字幕在线网 | 超碰人人射 | 一级成人欧美一区在线观看 | 久久精品国产久精国产 | 欧美黄成人免费网站大全 | 亚洲自拍第二页 | 国产精品久久久久久久久久免 | 91社区电影| av在线免费网址 | 性大片1000免费看 | 久久综合精品视频 | 亚洲视频精品在线 | 欧美一级做a | 看91 | 护士hd老师fre0性xxx | 香蕉国产9 | 一区二区三区欧美视频 | 久久久在线免费观看 |