原文:http://python.jobbole.com/81133/html
本文由 伯樂在線 - Den 翻譯,toolate 校稿。未經許可,禁止轉載!
英文出處:alstatr.blogspot.ca。歡迎加入翻譯組。python
最近,Analysis with Programming加入了Planet Python。做爲該網站的首批特約博客,我這裏來分享一下如何經過Python來開始數據分析。具體內容以下:git
這是很關鍵的一步,爲了後續的分析咱們首先須要導入數據。一般來講,數據是CSV格式,就算不是,至少也能夠轉換成CSV格式。在Python中,咱們的操做以下:程序員
1
2
3
4
5
6
7
8
|
import pandas as pd
# Reading data locally
df = pd.read_csv('/Users/al-ahmadgaidasaad/Documents/d.csv')
# Reading data from web
data_url = "https://raw.githubusercontent.com/alstat/Analysis-with-Programming/master/2014/Python/Numerical-Descriptions-of-the-Data/data.csv"
df = pd.read_csv(data_url)
|
爲了讀取本地CSV文件,咱們須要pandas這個數據分析庫中的相應模塊。其中的read_csv函數可以讀取本地和web數據。github
既然在工做空間有了數據,接下來就是數據變換。統計學家和科學家們一般會在這一步移除分析中的非必要數據。咱們先看看數據:web
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# Head of the data
print df.head()
# OUTPUT
Abra Apayao Benguet Ifugao Kalinga
0 1243 2934 148 3300 10553
1 4158 9235 4287 8063 35257
2 1787 1922 1955 1074 4544
3 17152 14501 3536 19607 31687
4 1266 2385 2530 3315 8520
# Tail of the data
print df.tail()
# OUTPUT
Abra Apayao Benguet Ifugao Kalinga
74 2505 20878 3519 19737 16513
75 60303 40065 7062 19422 61808
76 6311 6756 3561 15910 23349
77 13345 38902 2583 11096 68663
78 2623 18264 3745 16787 16900
|
對R語言程序員來講,上述操做等價於經過print(head(df))來打印數據的前6行,以及經過print(tail(df))來打印數據的後6行。固然Python中,默認打印是5行,而R則是6行。所以R的代碼head(df, n = 10),在Python中就是df.head(n = 10),打印數據尾部也是一樣道理。數組
在R語言中,數據列和行的名字經過colnames和rownames來分別進行提取。在Python中,咱們則使用columns和index屬性來提取,以下:dom
1
2
3
4
5
6
7
8
9
10
11
|
# 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 |