pandas庫學習筆記(一)Series入門學習


Pandas基本介紹:

pandas is an open source, BSD-licensed (permissive free software licenses) library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

 

我們快速簡單地看一下pandas中的基本數據結構,先從數據類型、索引、切片等簡單操作開始。首先導入必要的域名空間:

 
        
 

我們首先簡單介紹一下數據結構:

Series 是一個一維數組結構的,可以存入任一一種python的數據類型(integers, strings, floating point numbers, Python objects, etc.)。最創建一個Series的最基本方法是:

>>> s = pd.Series(data, index=index)

這里,data指代許多不同的數據類型:

  • a Python dict
  • an ndarray
  • a scalar value (like 5)

index指代一個標簽軸鏈表(list),因此,根據data的數據類型不同,我們可以大致有如下方式新建Series:

1、  from ndarray

如果data是ndarray類型,那么index的長度必須與data一樣。如果index值缺省,整數鏈表[0,1,2,…,len(data)-1]將會被自動初始化為index。

 

 

2、  from dict

如果data是字典結構,index默認為字典中的key值。如果在創建時index被重新賦值,那么value將會與新建的index對應,如果index值不在字典的key值中,那么value將會被初始化為NaN。

 

 

注:NaN不是一個值,在pandas中代表缺省值。

 

3、  from scalar value

如果data是一個標量,index值必須被初始化,value值將會重復對應到每一個index。

 

 

Series與ndarray類似

Series的操作與ndarray非常類似,但是Series可以應用numpy中的大多數函數,例如切片操作。

 

 

Series與dict類似

Series像一個固定大小的dict,可以通過index賦值或者取值。

 

Series矢量操作以及標簽對齊運算

在數據分析時,numpy無需進行循環即可對每一個值進行同等操作,Series也可以通過調用numpy中的函數達到預期運算結果。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM