python數據分析-Pandas數據結構(1)

聲明:此文只是對《對比excel,輕鬆學習python數據分析》這本書的讀書筆記,文章中的代碼均來源於書中,僅以知識分享爲用途,後續我會繼續支持作者,也會繼續讀完這本書,如有侵權,請留言我刪除

pandas數據結構

Series數據結構

  • 需要導入的庫
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
  • 什麼是Series數據結構
0    a
1    b
2    c
3    d
dtype: object

Series是一種類似於一維數組的對象,由一組數據及一組數據與之相關的數據標籤組成,像上面的這種數據結構就是Series,第一列數字是數據標籤,第二列是具體的數據,數據標籤與數據一一對應

  • 創建一個Series
# 利用pandas的Series()方法創建
S1 = pd.Series(['a', 'b', 'c', 'd'])
print(S1)

0    a
1    b
2    c
3    d
dtype: object

如果只是傳入一個列表不指定數據標籤,那麼Series會默認使用從0開始的數做數據標籤,上面的0.1.2.3就是默認的數據標籤

  • 指定索引

直接傳入一個列表會使用默認索引,也可以通過設置index參數來自定義索引

S1 = pd.Series([1, 2, 3, 4])     # 創建序列
S1.index = ['a', 'b', 'c', 'd']  # 通過index方法指定索引
print(S1)

a    1
b    2
c    3
d    4
dtype: int64
  • 傳入一個字典

也可以將數據與數據標籤以key,value的形式傳入,這樣字典裏的key就是數據標籤,value就是數據值

dic = {'a1': 1, 'b2': 2, 'c3': 3, 'd4': 4}
s1 = pd.Series(dic)
print(s1)

a1    1
b2    2
c3    3
d4    4
dtype: int64
  • 利用index方法獲取Series的索引

獲取一組數據的索引:利用index方法就可以獲取Series的索引值

S1 = pd.Series(['a', 'b', 'c', 'd'])
print(S1.index)
RangeIndex(start=0, stop=4, step=1)  # 連續數字提示起止位置及步長

dic = {'a1': 1, 'b2': 2, 'c3': 3, 'd4': 4}
s1 = pd.Series(dic)
print(s1.index)
Index(['a1', 'b2', 'c3', 'd4'], dtype='object')  # 自定義索引顯示索引值
  • 利用values方法獲取Series的值

與索引值對應的就是獲取Series的值,使用values方法

S1 = pd.Series(['a', 'b', 'c', 'd'])
print(S1.index)
[1 2 3 4]

dic = {'a1': 1, 'b2': 2, 'c3': 3, 'd4': 4}
s1 = pd.Series(dic)
print(s1.index)
['a' 'b' 'c' 'd']

DataFrame 表格型數據結構

DateFrame是什麼

Series是由一組數據與一組索引組成的數據結構,而DateFrame是由一組數據與一對索引組成的表格型數據結構

創建一個DateFrame

  • 傳入一個列表
df1 = pd.DataFrame(['a', 'b', 'c', 'd'])
print(df1)

   0
0  a
1  b
2  c
3  d

這裏只傳入了一個單一的列表,該列表的值就會顯示成一列,且行和列都是從0開始的默認索引

  • 傳入一個嵌套列表
df2 = pd.DataFrame([['a', 'A'], ['b', 'B'], ['c', 'C'], ['d', 'D']])
print(df2)

   0  1
0  a  A
1  b  B
2  c  C
3  d  D

當傳入一個嵌套列表時,會根據嵌套列表數顯示成多列數據,行,列索引同樣是從0開始的,列表裏嵌套的列表也可以換成元組

df2 = pd.DataFrame([('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')])
print(df2)

   0  1
0  a  A
1  b  B
2  c  C
3  d  D

  • 指定行,列索引

DateFrame()方法的行,列索引都是默認的,通過設置columns參數自定義列索引

# 設置列索引
df31 = pd.DataFrame([('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')], columns=['小寫', '大寫'])
print(df31)

  小寫 大寫
0  a  A
1  b  B
2  c  C
3  d  D

# 設置行索引
df32 = pd.DataFrame([('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')], index=['一', '二', '三', '四'])
print(df32)

   0  1
一  a  A
二  b  B
三  c  C
四  d  D
  • 傳入一個字典
date = {'小寫': ['a', 'b', 'c', 'd'], '大寫': ['A', 'B', 'C', 'D']}
df41 = pd.DataFrame(date)
print(df41)

  小寫 大寫
0  a  A
1  b  B
2  c  C
3  d  D

直接以字典的形式傳入DateFrame時,字典的key值就相當於列索引,如果沒有設置行索引,行索引默認還是從0開始,同時我們也可以設置行索引

date = {'小寫': ['a', 'b', 'c', 'd'], '大寫': ['A', 'B', 'C', 'D']}
df42 = pd.DataFrame(date, index=['一', '二', '三', '四'])
print(df42)

  小寫 大寫
一  a  A
二  b  B
三  c  C
四  d  D

獲取DateFrame的行,列索引

# 前例代碼
df2 = pd.DataFrame([['a', 'A'], ['b', 'B'], ['c', 'C'], ['d', 'D']])
df3 = pd.DataFrame([('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')], index=['一', '二', '三', '四'],
                  columns=['小寫', '大寫'])
# 用columns方法獲取列索引
print(df2.columns)
RangeIndex(start=0, stop=2, step=1)
print(df3.columns)
Index(['小寫', '大寫'], dtype='object')
# 用index方法獲取行索引
print(df2.index)
RangeIndex(start=0, stop=4, step=1)
print(df3.index)
Index(['一', '二', '三', '四'], dtype='object')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章