博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas.series的数据定位为什么用两个左中括号[[
阅读量:4145 次
发布时间:2019-05-25

本文共 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如下

 

pandas.Series

class pandas.Series(data=Noneindex=Nonedtype=Nonename=Nonecopy=Falsefastpath=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 pd

series1 = 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用法如下

  • df[col]:根据列名,并以Series的形式返回列
  • df[[col1, col2]]:以DataFrame形式返回多列
  • 引自

转载地址:http://upfti.baihongyu.com/

你可能感兴趣的文章
Eclipse 安装 Python插件,并运行
查看>>
MySql找不到指定文件,安装的目录
查看>>
Eclipse显示空白符,如空格、制表符、换行符的方法
查看>>
Eclipse配置 Anaconda
查看>>
MarkDown插入图片(使用Github图片床)
查看>>
Window丢失api-ms-win-crt-runtime-l1-1-0.dll
查看>>
Gradle不是内部或外部命令的报错提示
查看>>
Eclipse的Pydev代码格式化对齐
查看>>
Python基础-项目-day 7 mvc
查看>>
Python基础-day8 构建前端,让界面更丰富和好看
查看>>
MySQL的GUI工具
查看>>
Python基础-项目实战-day8新增API
查看>>
Android系统proc下查看cpuinfo的参数信息
查看>>
Java基础 01 第一个程序 Hello world
查看>>
Python基础-项目实战-day10 用户注册与登陆
查看>>
Android系统的获取 CPU 核数
查看>>
Android 系统 cpu0 目录查看 CPU 频率freq和策略policy
查看>>
Android 系统查看sys-devices-system-cpu-cpu0 目录结构
查看>>
Android系统 CPU 核数、频率、策略控制
查看>>
Python基础-Day 11 - 编写日志创建页
查看>>