您的位置:首页 > 编程语言 > Python开发

python pandas中Series内容学习笔记

2020-08-07 08:56 176 查看

pandas 中Series

Series=一维数组+数据标签(索引)

1. Series创建

Series()d输入可以是列表,可以是数组(各种numpy数据类型),当不指定索引时,x系统自动加上0-(N-1)的整数索引,还可以是字典类型,字典的键会被当成索引,字典的值会被当成Series的值。

import pandas as pd
s1=pd.Series([1,2,3,4,5])
print(s1)
####
0    1
1    2
2    3
3    4
4    5
dtype: int64

字典创建:

import pandas as pd
s1=pd.Series({'a':10,'b':20,'c':30,'d':40})
print(s1)
####
a    10
b    20
c    30
d    40
dtype: int64
import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
print(s1)
####
a    0
b    1
c    2
d    3
dtype: int32

2. Series 访问

  1. 通过索引访问包括索引切片
  2. 通过逻辑运算访问
  3. 通过.value和.index访问
import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
print(s1)
print(s1[2:3])
print(s1[1:4])
####
a    0
b    1
c    2
d    3
dtype: int32

c    2
dtype: int32

b    1
c    2
d    3
dtype: int32
import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
print(s1)
print(s1.values)
####
a    0
b    1
c    2
d    3
dtype: int32
[0 1 2 3]
import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
print(s1)
print(s1.index)
####
a    0
b    1
c    2
d    3
dtype: int32
Index(['a', 'b', 'c', 'd'], dtype='object')
import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
print(s1)
print(s1[s1>2])

####
a    0
b    1
c    2
d    3
dtype: int32
d    3
dtype: int32

3. Series 方法和属性以及修改

import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
s1['e']=np.nan
print(s1)
print(s1.isnull())
print(s1.notnull())
####
a    0.0
b    1.0
c    2.0
d    3.0
e    NaN
dtype: float64
a    False
b    False
c    False
d    False
e     True
dtype: bool
a     True
b     True
c     True
d     True
e    False
dtype: bool
import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
s1[1:3]=6
print(s1)
####
a    0
b    6
c    6
d    3
dtype: int32
import pandas as pd
import numpy as np
s1=pd.Series(np.arange(4),index=['a','b','c','d'])
s1.index=['A','B','C','D']
print(s1)
####
A    0
B    1
C    2
D    3
dtype: int32
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: