偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Pandas 數(shù)據(jù)分析:五大核心操作助你高效挖掘數(shù)據(jù)價(jià)值

大數(shù)據(jù) 數(shù)據(jù)分析
作為數(shù)據(jù)分析師日常工作的核心支撐工具,Pandas能輕松處理大規(guī)模結(jié)構(gòu)化數(shù)據(jù),執(zhí)行復(fù)雜轉(zhuǎn)換和聚合操作。本文將深入剖析Pandas數(shù)據(jù)分析中應(yīng)用頻率最高的五個(gè)核心操作。

Python的Pandas庫(kù)已成為數(shù)據(jù)分析領(lǐng)域的標(biāo)準(zhǔn)工具,其強(qiáng)大的DataFrame結(jié)構(gòu)讓數(shù)據(jù)處理變得前所未有的高效。作為數(shù)據(jù)分析師日常工作的核心支撐工具,Pandas能輕松處理大規(guī)模結(jié)構(gòu)化數(shù)據(jù),執(zhí)行復(fù)雜轉(zhuǎn)換和聚合操作。

本文將深入剖析Pandas數(shù)據(jù)分析中應(yīng)用頻率最高的五個(gè)核心操作。

1. 數(shù)據(jù)載入與探查:read_csv與基礎(chǔ)探查

(1) 數(shù)據(jù)獲取起點(diǎn)

import pandas as pd

# 從CSV文件加載數(shù)據(jù)
df = pd.read_csv('sales_data.csv', parse_dates=['order_date'])

# 基礎(chǔ)數(shù)據(jù)探查三步法
print("數(shù)據(jù)結(jié)構(gòu)概覽:")
print(df.info())

print("\n數(shù)據(jù)統(tǒng)計(jì)摘要:")
print(df.describe(include='all'))

print("\n首尾數(shù)據(jù)樣本:")
display(df.head(3), df.tail(2))

(2) 關(guān)鍵作用解析

  • read_csv:多參數(shù)控制日期解析、缺失值標(biāo)記、編碼格式
  • info():內(nèi)存優(yōu)化關(guān)鍵(顯示列數(shù)據(jù)類(lèi)型+內(nèi)存占用)
  • describe():自動(dòng)統(tǒng)計(jì)數(shù)值列的分布(均值、分位數(shù)等)
  • head()/tail():快速驗(yàn)證數(shù)據(jù)加載正確性

?? 實(shí)戰(zhàn)技巧:添加memory_usage='deep'參數(shù)可精確計(jì)算內(nèi)存占用,處理大表必備

2. 數(shù)據(jù)清洗與預(yù)處理:處理缺失值與重復(fù)項(xiàng)

(1) 數(shù)據(jù)質(zhì)量決定分析上限

# 缺失值分析
missing_matrix = df.isnull().sum()
print("缺失值統(tǒng)計(jì):\n", missing_matrix[missing_matrix > 0])

# 智能填充策略
df['product_category'].fillna('Unknown', inplace=True)  # 分類(lèi)變量填充
df['unit_price'] = df.groupby('region')['unit_price'].transform(
    lambda x: x.fillna(x.median()))  # 分組填充中位數(shù)

# 處理重復(fù)記錄
duplicates = df.duplicated(subset=['order_id'], keep=False)
print(f"發(fā)現(xiàn){duplicates.sum()}條疑似重復(fù)訂單")
df.drop_duplicates(subset=['order_id'], keep='first', inplace=True)

(2) 關(guān)鍵作用解析

  • isnull():布爾定位缺失值位置
  • fillna():差異化填充策略(常量、統(tǒng)計(jì)值、插值)
  • duplicated():精準(zhǔn)識(shí)別重復(fù)記錄
  • drop_duplicates():定制化刪除(保留首次/末次出現(xiàn))

?? 典型錯(cuò)誤:直接dropna()可能丟失有價(jià)值數(shù)據(jù),需結(jié)合業(yè)務(wù)判斷

3. 數(shù)據(jù)切片與篩選:loc/iloc與布爾索引

(1) 精確數(shù)據(jù)獲取技術(shù)

# 列選擇技巧
essential_cols = df.loc[:, ['order_date', 'customer_id', 'total_amount']]

# 復(fù)雜條件篩選
q3_high_value = df.loc[
    (df['order_date'].dt.quarter == 3) & 
    (df['total_amount'] > 1000) &
    (df['payment_status'] == 'completed')
]

# 混合索引演示
sample_data = df.iloc[10:20, [2, 5, 7]]  # 行號(hào)+列位置索引

(2) loc與iloc核心區(qū)別

特性

loc

iloc

索引類(lèi)型

標(biāo)簽索引(含列名)

純整數(shù)位置索引

切片包含

包含結(jié)束位置

不包含結(jié)束位置

布爾索引

完美支持

需轉(zhuǎn)換布爾數(shù)組

?? 進(jìn)階技巧:使用query()方法實(shí)現(xiàn)類(lèi)SQL表達(dá)式過(guò)濾:df.query("total_amount > 500 and region in ['East','West']")

4. 數(shù)據(jù)變形與重組:groupby聚合與pivot_table

(1) 維度分析黃金組合

# 基礎(chǔ)分組統(tǒng)計(jì)
region_sales = df.groupby('region')['total_amount'].agg(
    total_sales='sum',
    avg_order='mean',
    order_count='count'
).reset_index()

# 多維透視分析
pivot = pd.pivot_table(df,
                       index='product_category',
                       columns=df.order_date.dt.month,
                       values='quantity',
                       aggfunc='sum',
                       fill_value=0,
                       margins=True)

(2) 核心參數(shù)解析

agg():多函數(shù)聚合(可自定義聚合邏輯)

pivot_table參數(shù):

  • index/columns:行列維度
  • values:聚合指標(biāo)
  • aggfunc:聚合方式(sum/mean等)
  • margins:添加總計(jì)行/列
5. 時(shí)間序列處理:重采樣與滑動(dòng)窗口

(1) 時(shí)間維度深度分析

# 日期維度轉(zhuǎn)換
df['year_month'] = df['order_date'].dt.to_period('M')

# 月度重采樣分析(時(shí)間序列)
monthly_sales = df.set_index('order_date').resample('M')['total_amount'].sum()

# 滑動(dòng)窗口分析(趨勢(shì)觀察)
rolling_avg = monthly_sales.rolling(window=3, min_periods=1).mean()

(2) 關(guān)鍵方法對(duì)比

方法

應(yīng)用場(chǎng)景

典型函數(shù)

resample

頻率轉(zhuǎn)換(天→周/月)

sum/mean/max等

rolling

移動(dòng)窗口計(jì)算(滾動(dòng)平均/求和)

mean/sum/std

expanding

累積計(jì)算(YTD累計(jì))

cumsum/cumprod

結(jié)語(yǔ):從操作到洞見(jiàn)的躍遷

通過(guò)掌握這五大Pandas核心操作,您已完成數(shù)據(jù)分析工作流的閉環(huán)建設(shè)。但需謹(jǐn)記:技術(shù)僅是工具,真正的價(jià)值在于如何通過(guò)數(shù)據(jù)解決業(yè)務(wù)問(wèn)題:

  • 80/20法則:工作中80%的數(shù)據(jù)需求可通過(guò)這5類(lèi)操作實(shí)現(xiàn)
  • 組合創(chuàng)新:多操作集成解決復(fù)雜需求(如分組后清洗)
  • 性能優(yōu)化:大數(shù)據(jù)集時(shí)關(guān)注向量化操作,避免原生循環(huán)
責(zé)任編輯:趙寧寧 來(lái)源: Python數(shù)智工坊
相關(guān)推薦

2025-08-01 06:10:00

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

2025-07-21 05:55:00

2020-08-06 07:00:00

數(shù)據(jù)分析技術(shù)IT

2025-07-14 07:21:00

Pandas數(shù)據(jù)分析Python

2025-07-18 07:59:56

2021-12-24 08:18:01

CIO數(shù)據(jù)分析

2014-01-24 09:28:47

網(wǎng)絡(luò)安全大數(shù)據(jù)分析

2016-10-27 13:53:20

數(shù)據(jù)分析大數(shù)據(jù)

2025-04-16 08:10:00

PandasPython數(shù)據(jù)分析

2024-04-28 11:39:17

紹csvkit數(shù)據(jù)分析

2017-04-26 23:10:03

數(shù)據(jù)組織數(shù)據(jù)庫(kù)

2022-07-08 06:01:37

D-Tale輔助工具

2021-12-24 10:45:19

PandasLambda數(shù)據(jù)分析

2017-03-20 09:58:43

網(wǎng)絡(luò)數(shù)據(jù)分析工具

2023-11-15 18:03:11

Python數(shù)據(jù)分析基本工具

2022-04-19 08:00:00

數(shù)據(jù)分析數(shù)據(jù)科學(xué)大數(shù)據(jù)

2024-01-03 15:00:01

數(shù)據(jù)分析人工智能物聯(lián)網(wǎng)

2012-04-18 09:42:36

數(shù)據(jù)分析Hadoop

2011-05-24 14:34:16

網(wǎng)站數(shù)據(jù)分析

2017-01-12 17:19:02

數(shù)據(jù)中心葉脊架構(gòu)網(wǎng)絡(luò)標(biāo)準(zhǔn)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)