日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Pandas 基础 (1)—— Series

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pandas 基础 (1)—— Series 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. Pandas 介紹

Pandas 的名稱來自于面板數據(panel data)和 Python 數據分析(data analysis)。panel data是經濟學中關于多維數據集的一個術語,在 Pandas 中也提供了 panel 的數據類型。

pandas 的數據結構分類:

  • Series:一維數組,與 Numpy 中的一維 array 類似。二者與 Python 基本的數據結構 List 也很相近,其區別是:List 中的元素可以是不同的數據類型,而 Array 和 Series 中則只允許存儲相同的數據類型,這樣可以更有效的使用內存,提高運算效率。
  • Time- Series:以時間為索引的 Series。
  • DataFrame:二維的表格型數據結構。很多功能與 R 中的 data.frame 類似。可以將DataFrame 理解為 Series 的容器。以下的內容主要以 DataFrame 為主。
  • Panel :三維的數組,可以理解為 DataFrame 的容器。

使用下面這樣的 pandas 引入約定:

from pandas import  Series, DataFrameimport pandas as pd

2. Series 對象

Series 是一種類似于一維數組的對象,它由一組數據(各種 NumPy 數據類型)以及一組與之相關的數據標簽(即索引)組成。

In [114]: obj = Series([4,7,-5,3])In [115]: obj
Out[115]: 
0    4
1    7
2   -5
3    3
dtype: int64
# 通過 Series 的 values 和 index 屬性獲取其數組表示形式和索引對象
In [116]: obj.values
Out[116]: array([ 4,  7, -5,  3], dtype=int64)In [117]: obj.index
Out[117]: RangeIndex(start=0, stop=4, step=1)
# 使用 index 參數人為地設置索引符號
In [118]: obj2 = Series([4,7,-5,3],index=['a','b','c','d'])In [119]: obj2
Out[119]: 
a    4
b    7
c   -5
d    3
dtype: int64In [120]: obj2.index
Out[120]: Index([u'a', u'b', u'c', u'd'], dtype='object')

可以通過索引的方式獲取 Series 中的單個或一組值

In [121]: obj2['a']
Out[121]: 4In [122]: obj2['c'] = 100In [123]: obj2[['b','c','d']]
Out[123]: 
b      7
c    100
d      3
dtype: int64

數組運算(數組過濾、標量相乘、應用數學函數等)

In [125]: obj2[obj2 > 10]
Out[125]: 
c    100
dtype: int64In [126]: obj2 * 2
Out[126]: 
a      8
b     14
c    200
d      6
dtype: int64In [127]: np.exp(obj2)
Out[127]: 
a    5.459815e+01
b    1.096633e+03
c    2.688117e+43
d    2.008554e+01
dtype: float64

可以將 Series 看成是一個定長的有序字典,從而判斷索引鍵是否在數組中

In [128]: 'b' in obj2
Out[128]: TrueIn [129]: 'f' in obj2
Out[129]: False

通過字典來創建 Series

In [130]: d = {'a':100, 'b':200, 'c':300}In [131]: obj3 = Series(d)In [132]: obj3
Out[132]: 
a    100
b    200
c    300
dtype: int64In [133]: keys = ['a','b','c','d']In [134]: obj4 = Series(d,index=keys)In [135]: obj4
Out[135]: 
a    100.0
b    200.0
c    300.0
d      NaN
dtype: float64

由于 ‘d’ 所對應的值找不到,所以結果就會出現 NaN (not a number),在 pandas 中用于表示缺失或 NA 值

pandas 的 isnull 和 notnull 函數可用于檢測缺失數據

In [138]: pd.isnull(obj4)
Out[138]: 
a    False
b    False
c    False
d     True
dtype: boolIn [139]: pd.notnull(obj4)
Out[139]: 
a     True
b     True
c     True
d    False
dtype: bool
# Series 也有類似的實例方法
In [140]: obj4.isnull()
Out[140]: 
a    False
b    False
c    False
d     True
dtype: bool

Series 一個重要的功能:在算術運算中會自動對齊不同索引的數據

In [141]: obj3
Out[141]: 
a    100
b    200
c    300
dtype: int64In [142]: obj4
Out[142]: 
a    100.0
b    200.0
c    300.0
d      NaN
dtype: float64In [143]: obj3 + obj4
Out[143]: 
a    200.0
b    400.0
c    600.0
d      NaN
dtype: float64

Series 對象本身及其索引都有一個 name 屬性,可以隨意設置屬性值

In [144]: obj4.name = "This is obj4"In [145]: obj4.index.name = "obj4-index"In [146]: obj4
Out[146]: 
obj4-index
a    100.0
b    200.0
c    300.0
d      NaN
Name: This is obj4, dtype: float64

Series 的索引可以通過賦值的方式就地修改

In [147]: obj4.index = ['I', 'love', 'you', 'too']In [148]: obj4
Out[148]: 
I       100.0
love    200.0
you     300.0
too       NaN
Name: This is obj4, dtype: float64

總結

以上是生活随笔為你收集整理的Pandas 基础 (1)—— Series的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。