数据导入
需要pandas这个数据分析库中的相应模块。其中的read_csv函数能够读取本地和web数据。
import pandas as pd# Reading data locallydf = pd.read_csv('/Users/al-ahmadgaidasaad/Documents/d.csv')# Reading data from webdata_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)
数据变换
既然在工作空间有了数据,接下来就是数据变换。统计学家和科学家们通常会在这一步移除分析中的非必要数据。我们先看看数据:
# Head of the dataprint df.head()# OUTPUTAbra Apayao Benguet Ifugao Kalinga0 1243 2934 148 3300 105531 4158 9235 4287 8063 352572 1787 1922 1955 1074 45443 17152 14501 3536 19607 316874 1266 2385 2530 3315 8520# Tail of the dataprint df.tail()# OUTPUTAbra Apayao Benguet Ifugao Kalinga74 2505 20878 3519 19737 1651375 60303 40065 7062 19422 6180876 6311 6756 3561 15910 2334977 13345 38902 2583 11096 6866378 2623 18264 3745 16787 16900
Python中,默认打印是5行。df.head(n = 10),打印数据尾部也是同样道理。
在Python中,我们则使用columns和index属性来提取数据,如下:
# Extracting column namesprint df.columns# OUTPUTIndex([u'Abra', u'Apayao', u'Benguet', u'Ifugao', u'Kalinga'], dtype='object')# Extracting row names or the indexprint df.index# OUTPUTInt64Index([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')
数据转置使用T方法
# Transpose dataprint df.T# OUTPUT0 1 2 3 4 5 6 7 8 9Abra 1243 4158 1787 17152 1266 5576 927 21540 1039 5424Apayao 2934 9235 1922 14501 2385 7452 1099 17038 1382 10588Benguet 148 4287 1955 3536 2530 771 2796 2463 2592 1064Ifugao 3300 8063 1074 19607 3315 13134 5134 14226 6842 13828Kalinga 10553 35257 4544 31687 8520 28252 3106 36238 4973 40140... 69 70 71 72 73 74 75 76 77Abra ... 12763 2470 59094 6209 13316 2505 60303 6311 13345Apayao ... 37625 19532 35126 6335 38613 20878 40065 6756 38902Benguet ... 2354 4045 5987 3530 2585 3519 7062 3561 2583Ifugao ... 9838 17125 18940 15560 7746 19737 19422 15910 11096Kalinga ... 65782 15279 52437 24385 66148 16513 61808 23349 6866378Abra 2623Apayao 18264Benguet 3745Ifugao 16787Kalinga 16900Other transformations such as sort can be done using <code>sort</code> attribute. Now let's extract a specific column. In Python, we do it using either <code>iloc</code> or <code>ix</code> attributes, but <code>ix</code> is more robust and thus I prefer it. Assuming we want the head of the first column of the data, we have
其他变换,例如排序就是用sort属性。现在我们提取特定的某列数据。Python中,可以使用iloc或者ix属性。但是我更喜欢用ix,因为它更稳定一些。假设我们需数据第一列的前5行,我们有:
print df.ix[:, 0].head()# OUTPUT0 12431 41582 17873 171524 1266Name: Abra, dtype: int64
顺便提一下,Python的索引是从0开始而非1。为了取出从11到20行的前3列数据,我们有:
print df.ix[10:20, 0:3]# OUTPUTAbra Apayao Benguet10 981 1311 256011 27366 15093 303912 1100 1701 238213 7212 11001 108814 1048 1427 284715 25679 15661 294216 1055 2191 211917 5437 6461 73418 1029 1183 230219 23710 12222 259820 1091 2343 2654
上述命令相当于df.ix[10:20, ['Abra', 'Apayao', 'Benguet']]。
为了舍弃数据中的列,这里是列1(Apayao)和列2(Benguet),我们使用drop属性,如下:
print df.drop(df.columns[[1, 2]], axis = 1).head()# OUTPUTAbra Ifugao Kalinga0 1243 3300 105531 4158 8063 352572 1787 1074 45443 17152 19607 316874 1266 3315 8520
axis参数告诉函数到底舍弃列还是行。如果axis等于0,那么就舍弃行。
统计描述
下一步就是通过describe属性,对数据的统计特性进行描述:
print df.describe()# OUTPUTAbra Apayao Benguet Ifugao Kalingacount 79.000000 79.000000 79.000000 79.000000 79.000000mean 12874.379747 16860.645570 3237.392405 12414.620253 30446.417722std 16746.466945 15448.153794 1588.536429 5034.282019 22245.707692min 927.000000 401.000000 148.000000 1074.000000 2346.00000025% 1524.000000 3435.500000 2328.000000 8205.000000 8601.50000050% 5790.000000 10588.000000 3202.000000 13044.000000 24494.00000075% 13330.500000 33289.000000 3918.500000 16099.500000 52510.500000max 60303.000000 54625.000000 8813.000000 21031.000000 68663.000000
假设检验
Python有一个很好的统计推断包。那就是scipy里面的stats。ttest_1samp实现了单样本t检验。因此,如果我们想检验数据Abra列的稻谷产量均值,通过零假设,这里我们假定总体稻谷产量均值为15000,我们有:
from scipy import stats as ss# Perform one sample t-test using 1500 as the true meanprint ss.ttest_1samp(a = df.ix[:, 'Abra'], popmean = 15000)# OUTPUT(-1.1281738488299586, 0.26270472069109496)
