Pandas數(shù)據(jù)分析常用函數(shù)的使用
Pandas是數(shù)據(jù)處理和分析過程中常用的Python包,提供了大量能使我們快速便捷地處理數(shù)據(jù)的函數(shù)和方法,在此主要整理數(shù)據(jù)分析過程pandas包常用函數(shù),以便查詢。更多函數(shù)學(xué)習(xí)詳見padans官網(wǎng)
一、數(shù)據(jù)導(dǎo)入導(dǎo)出
pandas提供了一些用于將表格型數(shù)據(jù)讀取為DataFrame對(duì)象函數(shù),如read_csv,read_table。輸入pd.read后,按Tab鍵,系統(tǒng)將把以read開頭的函數(shù)和模塊都列出來,根據(jù)需要讀取的文件類型選取。
#包的安裝導(dǎo)入 import pandas as pd #查詢幫助文檔 pd.read_csv? #數(shù)據(jù)載入(僅羅列一部分常用參數(shù)) df = pd.read_csv( ? ? ?filePath, #路徑? ? ? ?sep=',', ?#分隔符 ? ? ?encoding='UTF-8', #用于unicode的文本編碼格式,如GBK,UTF-8 ? ? ?engine='python', ? ? ?header = None, #第一行不作為列名 ? ? ?names= [['col1','col2']], #字段名設(shè)置 ? ? ?index_col=None,? ? ? ?skiprows=None, #跳過行None ? ? ?error_bad_lines=False #錯(cuò)誤行忽略 ? ? ) # 數(shù)據(jù)導(dǎo)出 df.to_csv(filePath, ? ? ? ? ? ?sep = ',', ? ? ? ? ? ?index = False)
二、數(shù)據(jù)加工處理
1)重復(fù)值處理
# Pandas提供了duplicated、Index.duplicated、drop_duplicates函數(shù)來標(biāo)記及刪除重復(fù)記錄
#找出重復(fù)行位置
dIndex = df.duplicated()
#根據(jù)某些列找出重復(fù)位置
dIndex = df.duplicated('id')
dIndex = df.duplicated(['id', 'key'])
#根據(jù)返回值提取重復(fù)數(shù)據(jù)
df[dIndex]
#刪除重復(fù)行
newdf = df.drop_duplicated()
#去掉重復(fù)數(shù)據(jù)
newdf = df.drop_duplicated(keep = False)
#根據(jù)'key'字段去重,并保留重復(fù)key字段第一個(gè)
##subset:指定的標(biāo)簽或標(biāo)簽序列,僅刪除這些列重復(fù)值,默認(rèn)情況為所有列
##keep:確定要保留的重復(fù)值:first(保留第一次出現(xiàn)的重復(fù)值,默認(rèn))last(保留最后一次出現(xiàn)的重復(fù)值)False(刪除所有重復(fù)值)
newdf = df.drop_duplicated(subset = ['key'],keep = 'first')2)缺失值處理
# 輸出某列是否有為空值
print(df.isnull().any(axis = 0))
# 獲取空值所在的行
df[df.isnull().any(axis = 1)]
# 空值填充
df.fillna('未知')
# 刪除空值
newDF = dropna(axis="columns",how="all",inplace=False) #how可選有any和all,any表示只要有空值出現(xiàn)就刪除,all表示全部為空值才刪除,inplace表示是否替換掉原本數(shù)據(jù)
3)空格處理
newName = df['name'].str.lstrip() newName = df['name'].str.rstrip() newName = df['name'].str.strip()
4)字段拆分
newDF = df['name'].str.split(' ', 1, True)
5)篩選數(shù)據(jù)
#單條件
df[df.comments>10000]
#多條件
df[df.comments.between(1000, 10000)]
#過濾空值所在行
df[pandas.isnull(df.title)]
#根據(jù)關(guān)鍵字過濾
df[df.title.str.contains('臺(tái)電', na=False)]
#~為取反
df[~df.title.str.contains('臺(tái)電', na=False)]
#組合邏輯條件
df[(df.comments>=1000) & (df.comments<=10000)]
6)隨機(jī)抽樣
#設(shè)置隨機(jī)種子 numpy.random.seed(seed=2) #按照個(gè)數(shù)抽樣 data.sample(n=10) #按照百分比抽樣 data.sample(frac=0.02) #是否可放回抽樣, #replace=True,可放回, #replace=False,不可放回 data.sample(n=10, replace=True)
7)數(shù)據(jù)匹配
items = pandas.read_csv(
'D:\\PDA\\4.12\\data1.csv',
sep='|',
names=['id', 'comments', 'title']
)
prices = pandas.read_csv(
'D:\\PDA\\4.12\\data2.csv',
sep='|',
names=['id', 'oldPrice', 'nowPrice']
)
#默認(rèn)只是保留連接上的部分
itemPrices = pd.merge(
items,
prices,
left_on='id',
right_on='id',
how = 'left'
)
#how:連接方式,有inner、left、right、outer,默認(rèn)為inner;8)數(shù)據(jù)合并
data = pd.concat([data1, data2, data3])
9)時(shí)間處理
data['時(shí)間'] = pandas.to_datetime(
data.注冊(cè)時(shí)間,
format='%Y/%m/%d'
)
data['格式化時(shí)間'] = data.時(shí)間.dt.strftime('%Y-%m-%d')
data['時(shí)間.年'] = data['時(shí)間'].dt.year
data['時(shí)間.月'] = data['時(shí)間'].dt.month
data['時(shí)間.周'] = data['時(shí)間'].dt.weekday
data['時(shí)間.日'] = data['時(shí)間'].dt.day
data['時(shí)間.時(shí)'] = data['時(shí)間'].dt.hour
data['時(shí)間.分'] = data['時(shí)間'].dt.minute
data['時(shí)間.秒'] = data['時(shí)間'].dt.second
10)數(shù)據(jù)標(biāo)準(zhǔn)化
data['scale'] = round(
(
data.score-data.score.min()
)/(
data.score.max()-data.score.min()
)
, 2
)
11)修改列名和索引
#將id列設(shè)為索引
df = df.set_index('id')
12)排序
#選定列排序 df.sort_values(by=['age', 'gender'], ascending=[False, True], inplace=True, ignore_index=True)
三、列表格式設(shè)置
pd.set_option('display.max_rows',xxx) # 最大行數(shù)
pd.set_option('display.min_rows',xxx) # 最小顯示行數(shù)
pd.set_option('display.max_columns',xxx) # 最大顯示列數(shù)
pd.set_option ('display.max_colwidth',xxx) #最大列字符數(shù)
pd.set_option( 'display.precision',2) # 浮點(diǎn)型精度
pd.set_option('display.float_format','{:,}'.format) #逗號(hào)分隔數(shù)字
pd.set_option('display.float_format', ?'{:,.2f}'.format) #設(shè)置浮點(diǎn)精度
pd.set_option('display.float_format', '{:.2f}%'.format) #百分號(hào)格式化
pd.set_option('plotting.backend', 'altair') # 更改后端繪圖方式
pd.set_option('display.max_info_columns', 200) # info輸出最大列數(shù)
pd.set_option('display.max_info_rows', 5) # info計(jì)數(shù)null時(shí)的閾值
pd.describe_option() #展示所有設(shè)置和描述
pd.reset_option('all') #重置所有設(shè)置選項(xiàng)到此這篇關(guān)于Pandas數(shù)據(jù)分析常用函數(shù)的使用的文章就介紹到這了,更多相關(guān)Pandas數(shù)據(jù)分析常用函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python基礎(chǔ)教程之Pandas數(shù)據(jù)分析庫詳解
- Python數(shù)據(jù)分析pandas之布爾索引使用詳解
- Python+pandas數(shù)據(jù)分析實(shí)踐總結(jié)
- Python實(shí)踐之使用Pandas進(jìn)行數(shù)據(jù)分析
- Python?第三方庫?Pandas?數(shù)據(jù)分析教程
- Python利用Pandas進(jìn)行數(shù)據(jù)分析的方法詳解
- Pandas數(shù)據(jù)分析-pandas數(shù)據(jù)框的多層索引
- Pandas數(shù)據(jù)分析之pandas數(shù)據(jù)透視表和交叉表
- python pandas模塊進(jìn)行數(shù)據(jù)分析
相關(guān)文章
利用python庫在局域網(wǎng)內(nèi)傳輸文件的方法
今天小編就為大家分享一篇利用python庫在局域網(wǎng)內(nèi)傳輸文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
python使用兩種發(fā)郵件的方式smtp和outlook示例
本篇文章主要介紹了python使用兩種發(fā)郵件的方式smtp和outlook示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-06-06
python curl2pyreqs 生成接口腳本實(shí)戰(zhàn)教程
這篇文章主要介紹了python curl2pyreqs 生成接口腳本實(shí)戰(zhàn)教程,首先下載 curl2pyreqs 庫,打開調(diào)試模式,在Network這里獲取接口的cURL,需要的朋友可以參考下2023-10-10
python操作MySQL 模擬簡單銀行轉(zhuǎn)賬操作
這篇文章主要介紹了python操作MySQL 模擬簡單銀行轉(zhuǎn)賬操作,需要的朋友可以參考下2017-09-09
python深度學(xué)習(xí)tensorflow實(shí)例數(shù)據(jù)下載與讀取
這篇文章主要為大家介紹了python深度學(xué)習(xí)tensorflow實(shí)例數(shù)據(jù)下載與讀取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
用Python給圖像算法做個(gè)簡單應(yīng)用界面
這篇文章主要介紹了用Python給圖像算法做個(gè)簡單應(yīng)用界面,幫助大家更好的理解和學(xué)習(xí)使用python開發(fā)gui,感興趣的朋友可以了解下2021-05-05
PyCharm添加Anaconda中的虛擬環(huán)境Python解釋器出現(xiàn)Conda?executable?is?not
這篇文章主要給大家介紹了關(guān)于PyCharm添加Anaconda中的虛擬環(huán)境Python解釋器出現(xiàn)Conda?executable?is?not?found錯(cuò)誤的解決辦法,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02

