目錄
- 1 Series
- 1.1創(chuàng)造一個(gè)serise數(shù)據(jù)
- 1.2 指定index
- 1.3 用dictionary構(gòu)造一個(gè)series
- 1.4 用numpy ndarray構(gòu)造一個(gè)Series
- 1.5 選擇數(shù)據(jù)
- 1.6 操作數(shù)據(jù)
- 1.7 查找
- 1.8 Series賦值
- 1.9 滿足條件的統(tǒng)一賦值
1 Series
線性的數(shù)據(jù)結(jié)構(gòu), series是一個(gè)一維數(shù)組
Pandas 會(huì)默然用0到n-1來作為series的index, 但也可以自己指定index( 可以把index理解為dict里面的key )
1.1創(chuàng)造一個(gè)serise數(shù)據(jù)
import pandas as pd
import numpy as np
s = pd.Series([9, 'zheng', 'beijing', 128])
print(s)
打印
0 9
1 zheng
2 beijing
3 128
dtype: object
訪問其中某個(gè)數(shù)據(jù)
print(s[1:2])
# 打印
1 zheng
dtype: object
Series類型的基本操作:
Series類型包括index和values兩部分
In [14]: a = pd.Series({'a':1,'b':5})
In [15]: a.index
Out[15]: Index(['a', 'b'], dtype='object')
In [16]: a.values #返回一個(gè)多維數(shù)組numpy對(duì)象
Out[16]: array([1, 5], dtype=int64)
Series類型的操作類似ndarray類型
#自動(dòng)索引和自定義索引并存,但不能混用
In [17]: a[0] #自動(dòng)索引
Out[17]: 1
#自定義索引
In [18]: a['a']
Out[18]: 1
#不能混用
In [20]: a[['a',1]]
Out[20]:
a 1.0
1 NaN
dtype: float64
Series類型的操作類似Python字典類型
#通過自定義索引訪問
#對(duì)索引保留字in操作,值不可以
In [21]: 'a' in a
Out[21]: True
In [22]: 1 in a
Out[22]: False
Series類型在運(yùn)算中會(huì)自動(dòng)對(duì)齊不同索引的數(shù)據(jù)
In [29]: a = pd.Series([1,3,5],index = ['a','b','c'])
In [30]: b = pd.Series([2,4,5,6],index = ['c,','d','e','b'])
In [31]: a+b
Out[31]:
a NaN
b 9.0
c NaN
c, NaN
d NaN
e NaN
dtype: float64
Series對(duì)象可以隨時(shí)修改并即刻生效
In [32]: a.index = ['c','d','e']
In [33]: a
Out[33]:
c 1
d 3
e 5
dtype: int64
In [34]: a+b
Out[34]:
b NaN
c NaN
c, NaN
d 7.0
e 10.0
dtype: float64
1.2 指定index
import pandas as pd
import numpy as np
s = pd.Series([9, 'zheng', 'beijing', 128, 'usa', 990], index=[1,2,3,'e','f','g'])
print(s)
打印
1 9
2 zheng
3 beijing
e 128
f usa
g 990
dtype: object
根據(jù)索引找出值
1.3 用dictionary構(gòu)造一個(gè)series
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "car": None}
sa = pd.Series(s, name="age")
print(sa)
打印
car NaN
jack 19.0
mary 18.0
ton 20.0
Name: age, dtype: float64
檢測(cè)類型
print(type(sa)) # class 'pandas.core.series.Series'>
1.4 用numpy ndarray構(gòu)造一個(gè)Series
生成一個(gè)隨機(jī)數(shù)
import pandas as pd
import numpy as np
num_abc = pd.Series(np.random.randn(5), index=list('abcde'))
num = pd.Series(np.random.randn(5))
print(num)
print(num_abc)
# 打印
0 -0.102860
1 -1.138242
2 1.408063
3 -0.893559
4 1.378845
dtype: float64
a -0.658398
b 1.568236
c 0.535451
d 0.103117
e -1.556231
dtype: float64
1.5 選擇數(shù)據(jù)
import pandas as pd
import numpy as np
s = pd.Series([9, 'zheng', 'beijing', 128, 'usa', 990], index=[1,2,3,'e','f','g'])
print(s[1:3]) # 選擇第1到3個(gè), 包左不包右 zheng beijing
print(s[[1,3]]) # 選擇第1個(gè)和第3個(gè), zheng 128
print(s[:-1]) # 選擇第1個(gè)到倒數(shù)第1個(gè), 9 zheng beijing 128 usa
1.6 操作數(shù)據(jù)
import pandas as pd
import numpy as np
s = pd.Series([9, 'zheng', 'beijing', 128, 'usa', 990], index=[1,2,3,'e','f','g'])
sum = s[1:3] + s[1:3]
sum1 = s[1:4] + s[1:4]
sum2 = s[1:3] + s[1:4]
sum3 = s[:3] + s[1:]
print(sum)
print(sum1)
print(sum2)
print(sum3)
打印
2 zhengzheng
3 beijingbeijing
dtype: object
2 zhengzheng
3 beijingbeijing
e 256
dtype: object
2 zhengzheng
3 beijingbeijing
e NaN
dtype: object
1 NaN
2 zhengzheng
3 beijingbeijing
e NaN
f NaN
g NaN
dtype: object
1.7 查找
是否存在
范圍查找
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}
sa = pd.Series(s, name="age")
print(sa[sa>19])
中位數(shù)
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}
sa = pd.Series(s, name="age")
print(sa.median()) # 20
判斷是否大于中位數(shù)
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}
sa = pd.Series(s, name="age")
print(sa>sa.median())
找出大于中位數(shù)的數(shù)
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}
sa = pd.Series(s, name="age")
print(sa[sa > sa.median()])
中位數(shù)
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}
sa = pd.Series(s, name="age")
more_than_midian = sa>sa.median()
print(more_than_midian)
print('---------------------')
print(sa[more_than_midian])
1.8 Series賦值
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}
sa = pd.Series(s, name="age")
print(s)
print('----------------')
sa['ton'] = 99
print(sa)
1.9 滿足條件的統(tǒng)一賦值
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}
sa = pd.Series(s, name="age")
print(s) # 打印原字典
print('---------------------') # 分割線
sa[sa>19] = 88 # 將所有大于19的同一改為88
print(sa) # 打印更改之后的數(shù)據(jù)
print('---------------------') # 分割線
print(sa / 2) # 將所有數(shù)據(jù)除以2
到此這篇關(guān)于pandas的Series類型與基本操作詳解的文章就介紹到這了,更多相關(guān)pandas Series基本操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 在python中pandas的series合并方法
- Pandas把dataframe或series轉(zhuǎn)換成list的方法
- python pandas 對(duì)series和dataframe的重置索引reindex方法
- pandas中的series數(shù)據(jù)類型詳解
- pandas 對(duì)series和dataframe進(jìn)行排序的實(shí)例
- pandas series序列轉(zhuǎn)化為星期幾的實(shí)例
- python pandas中對(duì)Series數(shù)據(jù)進(jìn)行軸向連接的實(shí)例
- Python3.5 Pandas模塊之Series用法實(shí)例分析
- 淺談Pandas Series 和 Numpy array中的相同點(diǎn)
- pandas 數(shù)據(jù)結(jié)構(gòu)之Series的使用方法
- 淺談Pandas:Series和DataFrame間的算術(shù)元素
- 使用Pandas的Series方法繪制圖像教程