欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pandas數(shù)據(jù)分析常用函數(shù)的使用

 更新時(shí)間:2023年01月16日 16:13:28   作者:我的小毛驢呢  
本文主要介紹了Pandas數(shù)據(jù)分析常用函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Pandas是數(shù)據(jù)處理和分析過(guò)程中常用的Python包,提供了大量能使我們快速便捷地處理數(shù)據(jù)的函數(shù)和方法,在此主要整理數(shù)據(jù)分析過(guò)程pandas包常用函數(shù),以便查詢。更多函數(shù)學(xué)習(xí)詳見(jiàn)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開(kāi)頭的函數(shù)和模塊都列出來(lái),根據(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, #跳過(guò)行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ù)來(lái)標(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)]
#過(guò)濾空值所在行
df[pandas.isnull(df.title)]
#根據(jù)關(guān)鍵字過(guò)濾
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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論