天天看點

python資料分析-pandas子產品基礎知識(2)

呀~部落客是正在學習資料分析的一員,記錄的是自己學習過程中總結的知識點,肯定有不完善的地方,如有問題可以私聊我改正,共同學習進步。希望大家都能保持學習的熱情,堅持自己,不斷超越自己!

部落格位址:qxi的部落格

還是可以先了解下基礎篇(1)喲:pandas子產品基礎知識(1)

上一篇總結的是pandas子產品中的Series對象,還是比較簡單内容也不多,現在就開始介紹比較重要的資料結構:

DataFrame

#這一篇主要總結的是DataFrame與Series的差別,DataFrame如何生成,以及一些最最基本的操作,最後還有DataFrame的排序問題。#

1、注意Series隻有

一列序列值

,左邊是索引,而DataFrame是二維的,可以有很多列序列值,每一列可以是不同的值類型(數值,字元串,布爾值等),DataFrame即有行索引又有列索引。看下面這個例子應該會好了解些:

import pandas as pd
s=pd.Series([[32,6,17,3],[12,3,5,-7]])
df=pd.DataFrame([[32,6,17,3],[12,3,5,-7]])
print(s) 
print(df)
           

運作結果:

0    [32, 6, 17, 3]
1    [12, 3, 5, -7]  #Series隻有一列序列值,左邊是索引
    0  1   2  3
0  32  6  17  3
1  12  3   5 -7  #DataFrame可以有多列,有行索引列索引
           

2、如何生成DataFrame?通常可以利用

numpy數組

或者

字典

來生成DataFrame。

例子中

np.arange(12).reshape(3,4)

就是numpy數組,

data

是字典,

利用index指定行索引,利用columns指定列索引。

import pandas as pd
import numpy as np
df1=pd.DataFrame(np.arange(12).reshape(3,4)) #預設索引
print(df1)
df2=pd.DataFrame(np.arange(12).reshape(3,4),index=['a','b','c'],columns=['d','e','f','g']) #指定索引
print(df2)
data={'sex':['男','女','男','女','女'],'name':['李明','曉青','小亮','少梅','欣欣'],'score':[83,76,89,90,95]}  #字典
df3=pd.DataFrame(data)
print(df3)
           

運作結果:

0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11  #預設索引從0開始
   d  e   f   g
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11 #指定索引結果
  sex   name   score
0   男   李明     83
1   女   曉青     76
2   男   小亮     89
3   女   少梅     90
4   女   欣欣     95  #利用字典生成
           

3、對DataFrame的基本操作,顯示每一列的資料類型、顯示出值,顯示行索引以及列索引。

import pandas as pd
data={'sex':['男','女','男','女','女'],'name':['李明','曉青','小亮','少梅','欣欣'],'score':[83,76,89,90,95]}  #字典
df=pd.DataFrame(data)
print(df)
print(df.dtypes) #每一列的資料類型
print(df.values) #顯示出值
print(df.index)  #顯示行索引
print(df.columns) #顯示列索引
           

運作結果:

sex    name  score
0   男   李明     83
1   女   曉青     76
2   男   小亮     89
3   女   少梅     90
4   女   欣欣     95
sex      object
name     object
score     int64
dtype: object
[['男' '李明' 83]
 ['女' '曉青' 76]
 ['男' '小亮' 89]
 ['女' '少梅' 90]
 ['女' '欣欣' 95]]
RangeIndex(start=0, stop=5, step=1) #行索引值
Index(['sex', 'name', 'score'], dtype='object')  #列索引值
           

4、DataFrame的描述統計以及

轉置

import pandas as pd
data={'sex':['男','女','男','女','女'],'name':['李明','曉青','小亮','少梅','欣欣'],'chinese':[83,76,89,90,95],'matn':[88,90,87,78,95],'English':[77,98,90,87,85]}  #字典
df=pd.DataFrame(data)
print(df)
print(df.describe()) #描述統計量
print(df.T)  #轉置
           

運作結果:

sex name  chinese  matn  English
0   男   李明       83    88       77
1   女   曉青       76    90       98
2   男   小亮       89    87       90
3   女   少梅       90    78       87
4   女   欣欣       95    95       85  #原表
         chinese       matn    English
count   5.000000   5.000000   5.000000
mean   86.600000  87.600000  87.400000
std     7.300685   6.188699   7.635444
min    76.000000  78.000000  77.000000
25%    83.000000  87.000000  85.000000
50%    89.000000  88.000000  87.000000
75%    90.000000  90.000000  90.000000
max    95.000000  95.000000  98.000000  #描述統計一些量,由于前面列不是數值型,就沒有這些量
          0   1   2   3   4
sex       男   女   男   女   女
name     李明  曉青  小亮  少梅  欣欣
chinese   83  76  89  90  95
matn     88  90  87  78  95
English    77  98  90  87  85   #轉置結果
           

5、DataFrame的

排序問題

①sort_index(axis,ascending)函數是指按

索引

對資料進行排序,當axis=0表示逐行操作,針對

行索引

排序,1表示逐列操作,針對

列索引

排序;ascending=True是預設對索引升序排序,ascending=False是對索引降序排序。

②sort_values(by,ascending)是指按

某一列數值

進行排序,by引出哪一列,同樣ascending=False是降序排序。

import pandas as pd
data={'sex':['男','女','男','女','女'],'name':['李明','曉青','小亮','少梅','欣欣'],'chinese':[83,76,89,90,95],'matn':[88,90,87,78,95],'English':[77,98,90,87,85]}  #字典
df=pd.DataFrame(data)
df.columns=['A','B','C','D','E'] #對索引列修改
print(df)
print(df.sort_index(axis=0,ascending=False))  #逐行操作,對行索引降序
print(df.sort_index(axis=1,ascending=False))  #逐列操作,對列索引降序
print(df.sort_values(by='C',ascending=False))   #對C列數值降序排序
           

運作結果:

A   B   C   D   E
0  男  李明  83  88  77
1  女  曉青  76  90  98
2  男  小亮  89  87  90
3  女  少梅  90  78  87
4  女  欣欣  95  95  85  #修改之後的
   A   B   C   D   E
4  女  欣欣  95  95  85
3  女  少梅  90  78  87
2  男  小亮  89  87  90
1  女  曉青  76  90  98
0  男  李明  83  88  77 #行索引降序排序,變成4,3,2,1,0
    E   D   C   B  A
0  77  88  83  李明  男
1  98  90  76  曉青  女
2  90  87  89  小亮  男
3  87  78  90  少梅  女
4  85  95  95  欣欣  女  #列索引降序排序,變成E,D,C,B,A
   A   B   C   D   E
4  女  欣欣  95  95  85
3  女  少梅  90  78  87
2  男  小亮  89  87  90
0  男  李明  83  88  77
1  女  曉青  76  90  98  #對C列數值降序排序
           

好啦,今天的總結就到這裡啦。我覺得如果你是剛入門python資料分析的話,其實一開始對自己要求不能太高,得循序漸進,慢慢來,基礎易懂的東西才适合剛入門,這樣才不會容易被打擊,反而覺得挺有趣,因為你能看懂,啃得動哈哈,希望我的文章對你有幫助,關注我哈,我會持續更新的~