📜  Python Pandas Series

📅  最后修改于: 2020-10-29 01:36:03             🧑  作者: Mango

Python Pandas系列

Pandas系列可以定义为一维数组,能够存储各种数据类型。我们可以使用“ series”方法轻松地将列表,元组和字典转换为series。series的行标签称为索引。Series不能包含多列。它具有以下参数:

  • 数据:可以是任何列表,字典或标量值。
  • index:索引的值应唯一且可哈希。它必须与数据长度相同。如果我们不传递任何索引,则将使用默认的np.arrange(n)。
  • dtype:是指系列的数据类型。
  • 复制:用于复制数据。

创建系列:

我们可以通过两种方式创建系列:

  • 创建一个空系列
  • 使用输入创建系列。

创建一个空系列:

我们可以轻松地在Pandas中创建一个空系列,这意味着它将没有任何价值。

用于创建空序列的语法:

 = pandas.Series()

下面的示例创建一个没有值且具有默认数据类型(即float64)的Empty Series类型对象。

import pandas as pd
x = pd.Series()
print (x)

输出量

Series([], dtype: float64)

使用输入创建系列:

我们可以使用各种输入来创建系列:

  • 数组
  • 辞典
  • 标量值

从数组创建序列:

在创建系列之前,首先,我们必须导入numpy模块,然后在程序中使用array()函数。如果数据是ndarray,则传递的索引必须具有相同的长度。

如果我们不传递索引,则默认情况下会传递range(n)的索引,其中n定义数组的长度,即[0,1,2,…. range(len(array))- 1]。

import pandas as pd
import numpy as np
info = np.array(['P','a','n','d','a','s'])
a = pd.Series(info)
print(a) 

输出量

0    P
1    a
2    n
3    d
4    a
5    s
dtype: object

从字典创建系列

我们也可以根据字典创建系列。如果将字典对象作为输入传递而未指定索引,则按排序顺序获取字典键以构造索引。

如果传递了索引,则将从字典中提取与索引中特定标签相对应的值。

#import the pandas library 
import pandas as pd
import numpy as np
info = {'x' : 0., 'y' : 1., 'z' : 2.}
a = pd.Series(info)
print (a)

输出量

x     0.0
y     1.0
z     2.0
dtype: float64

使用标量创建系列:

如果采用标量值,则必须提供索引。标量值将被重复以匹配索引的长度。

#import pandas library 
import pandas as pd
import numpy as np
x = pd.Series(4, index=[0, 1, 2, 3])
print (x)

输出量

0      4
1      4
2      4
3      4
dtype: int64

通过位置访问系列数据:

创建Series类型对象后,就可以访问其索引,数据,甚至单个元素。

与ndarray中的数据类似,可以访问Series中的数据。

import pandas as pd
x = pd.Series([1,2,3],index = ['a','b','c'])
#retrieve the first element
print (x[0])

输出量

1 

系列对象属性

Series属性定义为与Series对象有关的任何信息,例如大小,数据类型。等。以下是一些可用于获取有关Series对象的信息的属性:

Attributes Description
Series.index Defines the index of the Series.
Series.shape It returns a tuple of shape of the data.
Series.dtype It returns the data type of the data.
Series.size It returns the size of the data.
Series.empty It returns True if Series object is empty, otherwise returns false.
Series.hasnans It returns True if there are any NaN values, otherwise returns false.
Series.nbytes It returns the number of bytes in the data.
Series.ndim It returns the number of dimensions in the data.
Series.itemsize It returns the size of the datatype of item.

检索系列对象的索引数组和数据数组

我们可以使用属性索引和值来检索现有Series对象的索引数组和数据数组。

import numpy as np 
import pandas as pd 
x=pd.Series(data=[2,4,6,8]) 
y=pd.Series(data=[11.2,18.6,22.5], index=['a','b','c']) 
print(x.index) 
print(x.values) 
print(y.index) 
print(y.values)

输出量

RangeIndex(start=0, stop=4, step=1)
[2 4 6 8]
Index(['a', 'b', 'c'], dtype='object')
[11.2 18.6 22.5]

检索类型(dtype)和类型大小(itemsize)

您可以将dtype属性与Series对象一起使用dtype用于检索系列对象的单个元素的数据类型,可以使用itemsize属性显示分配给每个数据项的字节数。

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,4]) 
b=pd.Series(data=[4.9,8.2,5.6], 
index=['x','y','z']) 
print(a.dtype) 
print(a.itemsize)  
print(b.dtype) 
print(b.itemsize)

输出量

int64
8
float64
8

检索形状

Series对象的形状定义元素的总数,包括缺失值或空值(NaN)。

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,4]) 
b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z']) 
print(a.shape) 
print(b.shape)

输出量

(4,)
(3,)

检索尺寸,大小和字节数:

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,4]) 
b=pd.Series(data=[4.9,8.2,5.6],
index=['x','y','z']) 
print(a.ndim, b.ndim) 
print(a.size, b.size) 
print(a.nbytes, b.nbytes)

输出量

1 1
4 3
32 24

检查空洞和NaN的存在

要检查Series对象是否为空,可以使用empty属性。同样,要检查系列对象是否包含某些NaN值,可以使用hasans属性。

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,np.NaN]) 
b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z']) 
c=pd.Series() 
print(a.empty,b.empty,c.empty) 
print(a.hasnans,b.hasnans,c.hasnans) 
print(len(a),len(b)) 
print(a.count( ),b.count( ))

输出量

False   False   True
True    False   False
4   3
3   3

系列功能

系列中使用的一些功能如下:

Functions Description
Pandas Series.map() Map the values from two series that have a common column.
Pandas Series.std() Calculate the standard deviation of the given set of numbers, DataFrame, column, and rows.
Pandas Series.to_frame() Convert the series object to the dataframe.
Pandas Series.value_counts() Returns a Series that contain counts of unique values.