本文共 2369 字,大约阅读时间需要 7 分钟。
实验数据
import pandas as pd
import numpy as np s = pd.Series(data=[1,2,3,4,np.NaN,np.NaN],index=["a","b","c","d","e","f"]) print(type(s)) s[[True,True,False,False,True,True]] s[["a","b"]]#result
Out[18]:
a 1.0b 2.0dtype: float64
若采用s['a','b']形式则会报错
TypeError: 'tuple' object cannot be interpreted as an integer
改成
s[['a','b']]则正常返回说明series后跟的数据类型不能是元组形式的,要么一个中括号[]里面一个列名或索引值,要么后面2个中括号[[]],里面是多个列值
#结论
可见s类型是series的,那么series()在选择数据时候,后面参数是什么类型的要么标量一个嵌套[],要么2层[[]],里面参数是列名的列表
比如上例
s["a"]则可以答案为
1.0
进一步查看官方API如下
class pandas.
Series
(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
One-dimensional ndarray with axis labels (including time series).
Labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Statistical methods from ndarray have been overridden to automatically exclude missing data (currently represented as NaN).
Operations between Series (+, -, /, , *) align values based on their associated index values– they need not be the same length. The result index will be the sorted union of the two indexes.
Parameters
data array-like, Iterable, dict, or scalar value
Contains data stored in Series.
Changed in version 0.23.0: If data is a dict, argument order is maintained for Python 3.6 and later.
indexarray-like or Index (1d)
Values must be hashable and have the same length as data. Non-unique index values are allowed. Will default to RangeIndex (0, 1, 2, …, n) if not provided. If both a dict and index sequence are used, the index will override the keys found in the dict.
dtypestr, numpy.dtype, or ExtensionDtype, optional
Data type for the output Series. If not specified, this will be inferred from data. See the for more usages.
namestr, optional
The name to give to the Series.
copybool, default False
Copy input data.
#########################################################
series对象支持查询操作
Series对象支持查询方式:
位置下标 标签索引 切片索引 布尔型索引 from pandas import Series import pandas as pdseries1 = Series([10, 20, 30, 40], index=list('abcd'))
# 通过位置查询
series1[2] # 通过标签索引查询 series1['b'] # 查询多个元素 series1[[0, 2, 3]] series1[['a', 'c', 'd']] # 切片 series1[1:3] series1['b':'d'] # 布尔索引 series1[series1 > 20]引自
官方API用法如下
转载地址:http://upfti.baihongyu.com/