使用python實現etl過程的簡單介紹

本文目錄一覽:

數據分析員用python做數據分析是怎麼回事,需要用到python中的那些內容,具體是怎麼操作的?

最近,Analysis with Programming加入了Planet Python。我這裡來分享一下如何通過Python來開始數據分析。具體內容如下:

數據導入

導入本地的或者web端的CSV文件;

數據變換;

數據統計描述;

假設檢驗

單樣本t檢驗;

可視化;

創建自定義函數。

數據導入

1

這是很關鍵的一步,為了後續的分析我們首先需要導入數據。通常來說,數據是CSV格式,就算不是,至少也可以轉換成CSV格式。在Python中,我們的操作如下:

import pandas as pd

# Reading data locally

df = pd.read_csv(‘/Users/al-ahmadgaidasaad/Documents/d.csv’)

# Reading data from web

data_url = “”

df = pd.read_csv(data_url)

為了讀取本地CSV文件,我們需要pandas這個數據分析庫中的相應模塊。其中的read_csv函數能夠讀取本地和web數據。

END

數據變換

1

既然在工作空間有了數據,接下來就是數據變換。統計學家和科學家們通常會在這一步移除分析中的非必要數據。我們先看看數據(下圖)

對R語言程序員來說,上述操作等價於通過print(head(df))來列印數據的前6行,以及通過print(tail(df))來列印數據的後6行。當然Python中,默認列印是5行,而R則是6行。因此R的代碼head(df, n = 10),在Python中就是df.head(n = 10),列印數據尾部也是同樣道理

請點擊輸入圖片描述

2

在R語言中,數據列和行的名字通過colnames和rownames來分別進行提取。在Python中,我們則使用columns和index屬性來提取,如下:

# Extracting column names

print df.columns

# OUTPUT

Index([u’Abra’, u’Apayao’, u’Benguet’, u’Ifugao’, u’Kalinga’], dtype=’object’)

# Extracting row names or the index

print df.index

# OUTPUT

Int64Index([0, 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, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78], dtype=’int64′)

3

數據轉置使用T方法,

# Transpose data

print df.T

# OUTPUT

0      1     2      3     4      5     6      7     8      9

Abra      1243   4158  1787  17152  1266   5576   927  21540  1039   5424

Apayao    2934   9235  1922  14501  2385   7452  1099  17038  1382  10588

Benguet    148   4287  1955   3536  2530    771  2796   2463  2592   1064

Ifugao    3300   8063  1074  19607  3315  13134  5134  14226  6842  13828

Kalinga  10553  35257  4544  31687  8520  28252  3106  36238  4973  40140

…       69     70     71     72     73     74     75     76     77

Abra     …    12763   2470  59094   6209  13316   2505  60303   6311  13345

Apayao   …    37625  19532  35126   6335  38613  20878  40065   6756  38902

Benguet  …     2354   4045   5987   3530   2585   3519   7062   3561   2583

Ifugao   …     9838  17125  18940  15560   7746  19737  19422  15910  11096

Kalinga  …    65782  15279  52437  24385  66148  16513  61808  23349  68663

78

Abra      2623

Apayao   18264

Benguet   3745

Ifugao   16787

Kalinga  16900

Other transformations such as sort can be done using codesort/code attribute. Now let’s extract a specific column. In Python, we do it using either codeiloc/code or codeix/code attributes, but codeix/code is more robust and thus I prefer it. Assuming we want the head of the first column of the data, we have

4

其他變換,例如排序就是用sort屬性。現在我們提取特定的某列數據。Python中,可以使用iloc或者ix屬性。但是我更喜歡用ix,因為它更穩定一些。假設我們需數據第一列的前5行,我們有:

print df.ix[:, 0].head()

# OUTPUT 0     1243 1     4158 2     1787 3    17152 4     1266 Name: Abra, dtype: int64

5

順便提一下,Python的索引是從0開始而非1。為了取出從11到20行的前3列數據,我們有

print df.ix[10:20, 0:3]

# OUTPUT

Abra  Apayao  Benguet

10    981    1311     2560

11  27366   15093     3039

12   1100    1701     2382

13   7212   11001     1088

14   1048    1427     2847

15  25679   15661     2942

16   1055    2191     2119

17   5437    6461      734

18   1029    1183     2302

19  23710   12222     2598

20   1091    2343     2654

上述命令相當於df.ix[10:20, [‘Abra’, ‘Apayao’, ‘Benguet’]]。

6

為了捨棄數據中的列,這裡是列1(Apayao)和列2(Benguet),我們使用drop屬性,如下:

print df.drop(df.columns[[1, 2]], axis = 1).head()

# OUTPUT

Abra  Ifugao  Kalinga

0   1243    3300    10553

1   4158    8063    35257

2   1787    1074     4544

3  17152   19607    31687

4   1266    3315     8520

axis 參數告訴函數到底捨棄列還是行。如果axis等於0,那麼就捨棄行。

END

統計描述

1

下一步就是通過describe屬性,對數據的統計特性進行描述:

print df.describe()

# OUTPUT

Abra        Apayao      Benguet        Ifugao       Kalinga

count     79.000000     79.000000    79.000000     79.000000     79.000000

mean   12874.379747  16860.645570  3237.392405  12414.620253  30446.417722

std    16746.466945  15448.153794  1588.536429   5034.282019  22245.707692

min      927.000000    401.000000   148.000000   1074.000000   2346.000000

25%     1524.000000   3435.500000  2328.000000   8205.000000   8601.500000

50%     5790.000000  10588.000000  3202.000000  13044.000000  24494.000000

75%    13330.500000  33289.000000  3918.500000  16099.500000  52510.500000

max    60303.000000  54625.000000  8813.000000  21031.000000  68663.000000

END

假設檢驗

1

Python有一個很好的統計推斷包。那就是scipy裡面的stats。ttest_1samp實現了單樣本t檢驗。因此,如果我們想檢驗數據Abra列的稻穀產量均值,通過零假設,這裡我們假定總體稻穀產量均值為15000,我們有:

from scipy import stats as ss

# Perform one sample t-test using 1500 as the true mean

print ss.ttest_1samp(a = df.ix[:, ‘Abra’], popmean = 15000)

# OUTPUT

(-1.1281738488299586, 0.26270472069109496)

返回下述值組成的元祖:

t : 浮點或數組類型t統計量

prob : 浮點或數組類型two-tailed p-value 雙側概率值

2

通過上面的輸出,看到p值是0.267遠大於α等於0.05,因此沒有充分的證據說平均稻穀產量不是150000。將這個檢驗應用到所有的變數,同樣假設均值為15000,我們有:

print ss.ttest_1samp(a = df, popmean = 15000)

# OUTPUT

(array([ -1.12817385,   1.07053437, -65.81425599,  -4.564575  ,   6.17156198]),

array([  2.62704721e-01,   2.87680340e-01,   4.15643528e-70,

1.83764399e-05,   2.82461897e-08]))

第一個數組是t統計量,第二個數組則是相應的p值

END

可視化

1

Python中有許多可視化模塊,最流行的當屬matpalotlib庫。稍加提及,我們也可選擇bokeh和seaborn模塊。之前的博文中,我已經說明了matplotlib庫中的盒須圖模塊功能。

請點擊輸入圖片描述

2

# Import the module for plotting

import matplotlib.pyplot as plt

plt.show(df.plot(kind = ‘box’))

現在,我們可以用pandas模塊中集成R的ggplot主題來美化圖表。要使用ggplot,我們只需要在上述代碼中多加一行,

import matplotlib.pyplot as plt

pd.options.display.mpl_style = ‘default’ # Sets the plotting display theme to ggplot2

df.plot(kind = ‘box’)

3

這樣我們就得到如下圖表:

請點擊輸入圖片描述

4

比matplotlib.pyplot主題簡潔太多。但是在本文中,我更願意引入seaborn模塊,該模塊是一個統計數據可視化庫。因此我們有:

# Import the seaborn library

import seaborn as sns

# Do the boxplot

plt.show(sns.boxplot(df, widths = 0.5, color = “pastel”))

請點擊輸入圖片描述

5

多性感的盒式圖,繼續往下看。

請點擊輸入圖片描述

6

plt.show(sns.violinplot(df, widths = 0.5, color = “pastel”))

請點擊輸入圖片描述

7

plt.show(sns.distplot(df.ix[:,2], rug = True, bins = 15))

請點擊輸入圖片描述

8

with sns.axes_style(“white”):

plt.show(sns.jointplot(df.ix[:,1], df.ix[:,2], kind = “kde”))

請點擊輸入圖片描述

9

plt.show(sns.lmplot(“Benguet”, “Ifugao”, df))

END

創建自定義函數

在Python中,我們使用def函數來實現一個自定義函數。例如,如果我們要定義一個兩數相加的函數,如下即可:

def add_2int(x, y):

return x + y

print add_2int(2, 2)

# OUTPUT

4

順便說一下,Python中的縮進是很重要的。通過縮進來定義函數作用域,就像在R語言中使用大括弧{…}一樣。這有一個我們之前博文的例子:

產生10個正態分布樣本,其中和

基於95%的置信度,計算和 ;

重複100次; 然後

計算出置信區間包含真實均值的百分比

Python中,程序如下:

import numpy as np

import scipy.stats as ss

def case(n = 10, mu = 3, sigma = np.sqrt(5), p = 0.025, rep = 100):

m = np.zeros((rep, 4))

for i in range(rep):

norm = np.random.normal(loc = mu, scale = sigma, size = n)

xbar = np.mean(norm)

low = xbar – ss.norm.ppf(q = 1 – p) * (sigma / np.sqrt(n))

up = xbar + ss.norm.ppf(q = 1 – p) * (sigma / np.sqrt(n))

if (mu low) (mu up):

rem = 1

else:

rem = 0

m[i, :] = [xbar, low, up, rem]

inside = np.sum(m[:, 3])

per = inside / rep

desc = “There are ” + str(inside) + ” confidence intervals that contain “

“the true mean (” + str(mu) + “), that is ” + str(per) + ” percent of the total CIs”

return {“Matrix”: m, “Decision”: desc}

上述代碼讀起來很簡單,但是循環的時候就很慢了。下面針對上述代碼進行了改進,這多虧了 Python專家

import numpy as np

import scipy.stats as ss

def case2(n = 10, mu = 3, sigma = np.sqrt(5), p = 0.025, rep = 100):

scaled_crit = ss.norm.ppf(q = 1 – p) * (sigma / np.sqrt(n))

norm = np.random.normal(loc = mu, scale = sigma, size = (rep, n))

xbar = norm.mean(1)

low = xbar – scaled_crit

up = xbar + scaled_crit

rem = (mu low) (mu up)

m = np.c_[xbar, low, up, rem]

inside = np.sum(m[:, 3])

per = inside / rep

desc = “There are ” + str(inside) + ” confidence intervals that contain “

“the true mean (” + str(mu) + “), that is ” + str(per) + ” percent of the total CIs”

return {“Matrix”: m, “Decision”: desc}

利用python實現數據分析

鏈接:

提取碼:7234

煉數成金:Python數據分析。Python是一種面向對象、直譯式計算機程序設計語言。也是一種功能強大而完善的通用型語言,已經具有十多年的發展歷史,成熟且穩定。Python 具有腳本語言中最豐富和強大的類庫,足以支持絕大多數日常應用。 Python語法簡捷而清晰,具有豐富和強大的類庫。它常被昵稱為膠水語言,它能夠很輕鬆的把用其他語言製作的各種模塊(尤其是C/C++)輕鬆地聯結在一起。

課程將從Python的基本使用方法開始,一步步講解,從ETL到各種數據分析方法的使用,並結合實例,讓學員能從中借鑒學習。

課程目錄:

Python基礎

Python的概覽——Python的基本介紹、安裝與基本語法、變數類型與運算符

了解Python流程式控制制——條件、循環語句與其他語句

常用函數——函數的定義與使用方法、主要內置函數的介紹

…..

python可以做數據分析,好處是什麼呢?怎麼學習?

鏈接:

提取碼:7234

煉數成金:Python數據分析。Python是一種面向對象、直譯式計算機程序設計語言。也是一種功能強大而完善的通用型語言,已經具有十多年的發展歷史,成熟且穩定。Python 具有腳本語言中最豐富和強大的類庫,足以支持絕大多數日常應用。 Python語法簡捷而清晰,具有豐富和強大的類庫。它常被昵稱為膠水語言,它能夠很輕鬆的把用其他語言製作的各種模塊(尤其是C/C++)輕鬆地聯結在一起。

課程將從Python的基本使用方法開始,一步步講解,從ETL到各種數據分析方法的使用,並結合實例,讓學員能從中借鑒學習。

課程目錄:

Python基礎

Python的概覽——Python的基本介紹、安裝與基本語法、變數類型與運算符

了解Python流程式控制制——條件、循環語句與其他語句

常用函數——函數的定義與使用方法、主要內置函數的介紹

…..

有用Python寫ETL的嗎

1.目前沒見過吧,但應該有,可以在github上搜一下

2.目前見過最多的etl是java的,如果提供介面的,也可以用python封裝調用

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/204491.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-07 12:17
下一篇 2024-12-07 12:17

相關推薦

  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29

發表回復

登錄後才能評論