python資料分析之pandas(三)Index

2021-10-02 17:18:21 字數 1329 閱讀 2316

index物件宣告後不能改變,不同的資料結構公用index物件

1. index物件的方法

frame.idxmin()、frame.idxmax() //索引值最小和最大元素

2. 含有重複標籤的index

index=['a', 'a']

frame.index.is_unique //索引是否唯一

3. 更換索引

frame.reindex(['a', 'b'])

注意:會刪除在新index中不存在的標籤,重新排序

可以使用method='ffill/bfill',利用之前或之後的沒有缺失的索引值做插值

另外可同時更換columns

4. 刪除索引行資料

frame.drop('a') //刪除對應的索引行

frame.drop(['a', 'b'])

>>> s = pd.series([1,2,3], ['a', 'b', 'c'])

>>> s.drop('a')

b 2

c 3

dtype: int64

5.刪除列資料

frame.drop(['col1', 'col2'], axis=1) //1為列,0為刪除行

>>> s = pd.dataframe(np.arange(6).reshape(3,2), ['a', 'b', 'c'], columns=['c1',

'c2'])

>>> s

c1 c2

a 0 1

b 2 3

c 4 5

>>> s.drop('c1', axis=1)

c2a 1

b 3

c 5

>>>

6.算術和資料對齊

以求和為例,對於剛定義的兩個series物件,有些標籤兩者都有,有些只屬於其中乙個物件。如果乙個標籤,兩個series都有,就把他們的元素相加,反之,標籤也會xian顯示在結果(新series物件)中,只不過元素為nan

>>> import pandas as pd

>>> s1 = pd.series([1,2,3], ['a', 'b', 'c'])

>>> s2 = pd.series([4,5,6], ['a', 'b', 'd'])

>>> s1 + s2

a    5.0

b    7.0

c    nan

d    nan

dtype: float64

>>>

Python之資料分析(寶可夢資料分析)

在此感謝阿里雲天池平台提供的學習平台,並提供相應的教程供小白們學習資料分析。seaborn庫 seaborn 是基於 python 且非常受歡迎的圖形視覺化庫,在 matplotlib 的基礎上,進行了更高階的封裝,使得作圖更加方便快捷。即便是沒有什麼基礎的人,也能通過極簡的 做出具有分析價值而又十...

python資料分析之Numpy

numpy系統是python的一種開源的數值計算擴充套件 ndarray 多維陣列 所有元素必須是相同型別 ndim屬性,維度個數 shape屬性,各維度大小 dtype屬性,資料型別 coding utf 8 import numpy as np 生成指定維度的隨機多維資料 data np.ran...

Python 資料分析之scipy

scipy是一組專門解決科學計算中各種標準問題域的包的集合,主要包括下面這些包 匯入積分模組 import numpy as np 匯入numpy庫 from scipy import integrate 匯入定積分模組scipy.integrate.quad func,a,b 計算單重積分,引數分...