Python: 对于DataFrame.loc传入列表和传入元组输出区别的理解
def test():
import pandas as pd
tuples = [
('cobra', 'mark i'), ('cobra', 'mark ii'),
('sidewinder', 'mark i'), ('sidewinder', 'mark ii'),
('viper', 'mark ii'), ('viper', 'mark iii')
]
index = pd.MultiIndex.from_tuples(tuples)
values = [[12, 2], [0, 4], [10, 20], [1, 4], [7, 1], [16, 36]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
print(df)
all = slice(None)
print(df.loc[[all, "mark i"], :])
print(df.loc[(all, "mark i"), :])
上面代码中,传入列表[all, "mark i"]和传入元组(all, "mark i")的结果不一致。
其中,
df.loc中传入列表,相当于列表中的每一个元素都需要对df进行索引切片。第一个元素是all,这个all相当于匹配任何索引,所以所有的数据都打印出来了。
传入元组,则相当与只使用元组执行一次切片。all相当于匹配索引的外层所有索引,“mark i"相当于匹配内层值为mark i的索引,最后打印的数据只有两个。
DataFrame.loc API
DataFrame.loc是纯粹基于标签的标签索引选择器。
loc[]主要是基于标签的,但也可以与布尔数组一起使用。
支持的传参格式:
1. 一个单一的标签。比如5、‘g’.注意这里的5解释为索引中的一个标签,而不是沿着索引的整数位置。
2. 标签构成的列表和数组。[‘a’, 'b', 'c']
3. 标签组成的切片对象,比如 ‘a’: 'f' 。含头且含尾。
4. 传入布尔数组
5. 带有一个参数的可调用函数,返回可以用索引的结果。
Python: 对于DataFrame.loc传入列表和传入元组输出区别的理解的更多相关文章
- python pandas.DataFrame .loc,.iloc,.ix 用法
refer to: http://www.cnblogs.com/harvey888/p/6006200.html
- python基础2--数据结构(列表List、元组Tuple、字典Dict)
1.Print函数中文编码问题 print中的编码:# -*- coding: utf-8 -*- 注:此处的#代表的是配置信息 print中的换行符,与C语言相同,为"\n" 2 ...
- [Python]字典Dictionary、列表List、元组Tuple差异化理解
概述:Python中这三种形式的定义相近,易于混淆,应注意区分. aDict={'a':1, 'b':2, 'c':3, 'd':4, 'e':5} aList=[1,2,3,4,5] aTuple= ...
- (原)怎样解决python dataframe loc,iloc循环处理速度很慢的问题
怎样解决python dataframe loc,iloc循环处理速度很慢的问题 1.问题说明 最近用DataFrame做大数据 处理,发现处理速度特别慢,追究原因,发现是循环处理时,loc,iloc ...
- 怎样解决python dataframe loc,iloc循环处理速度很慢的问题
怎样解决python dataframe loc,iloc循环处理速度很慢的问题 1.问题说明 最近用DataFrame做大数据 处理,发现处理速度特别慢,追究原因,发现是循环处理时,loc,iloc ...
- python基础一数据类型之列表
摘要: python基础一中写到列表,那么这篇主要讲列表. 1,定义列表 2,列表.元祖.字符串都属于序列,都可以用用索引和切片. 3,列表的方法 1,定义列表 list1 = ['a','b',1, ...
- python整型-浮点型-字符串-列表及内置函数(上)
整型 简介 # 是否可变类型: 不可变类型 # 作用:记录年龄.手机号 # 定义: age = 18 # --> 内部操作 age = int(18) # int('sada') # 报错 in ...
- Python学习(四)—— 列表和元组的类中方法
列表 list:用中括号括起来,用“,”分割每个元素,列表中的元素可以是 数字.字符串.列表.布尔值......所有东西,可以说就是一个“集合” li = [1,3,5,'alex','age',[' ...
- Python pandas DataFrame操作
1. 从字典创建Dataframe >>> import pandas as pd >>> dict1 = {'col1':[1,2,5,7],'col2':['a ...
随机推荐
- Postgresql 解决锁表
转载地址:https://blog.csdn.net/cicon/article/details/68068462##一.postgresql解决锁表--查询是否锁表了select oid from ...
- syniverse是一家怎样的公司
syniverse是一家怎样的公司?(详见问题描述)? 李超 核心业务当然是国际漫游了.简单来说就是做全球各个运营商之间的hub. 打个比方说,一家运营商A做通信,它的覆盖范围肯定是有限的(比如中 ...
- 1059 Prime Factors(25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- Hadoop YARN 调度器(scheduler) —— 资源调度策略
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/hadoop_yarn_resource_scheduler 搜了 ...
- python2和python3区别
字符编码: py3中默认字符编码是unicode:py2中默认字符编码是 ASCII,如果文件中出现了中文,需要在顶部加入coding声明#coding:utf8 让用户输入:py3中直接使用inpu ...
- mac apache 管理
sudo apachectl start sudo apachectl stop sudo apachectl restart
- ros python 构造 pose
#!/usr/bin/env python import numpy as npfrom geometry_msgs.msg import Pose, Point, Quaternion, Twist ...
- 三十一、Gawk基础入门
AWK:Aho Weinberger Kernighan awk :报告生成器.格式化文本输出 一.gawk - pattern scanning and processing language 基本 ...
- Vue-cli 构建项目后 npm run build 如何在本地运行查看
当你在本地直接打开index.html 你会发现了一丢丢404,这时候你有两个办法解决问题: 1:改变路径为相对路径. 在config 文件夹中index.js的 build对象里, 把 assets ...
- 纯CSS画三角形(带边框)
实例一: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...