通过整数索引选择一行熊猫系列/数据框
本文翻译自:Selecting a row of pandas series/dataframe by integer index
I am curious as to why df[2]
is not supported, while df.ix[2]
and df[2:3]
both work. 我很好奇为什么不支持df[2]
,而df.ix[2]
和df[2:3]
都可以工作。
In [26]: df.ix[2]
Out[26]:
A 1.027680
B 1.514210
C -1.466963
D -0.162339
Name: 2000-01-03 00:00:00In [27]: df[2:3]
Out[27]: A B C D
2000-01-03 1.02768 1.51421 -1.466963 -0.162339
I would expect df[2]
to work the same way as df[2:3]
to be consistent with Python indexing convention. 我希望df[2]
与df[2:3]
工作方式相同,以符合Python索引约定。 Is there a design reason for not supporting indexing row by single integer? 是否有设计原因不支持按单个整数索引行?
#1楼
参考:https://stackoom.com/question/15XT1/通过整数索引选择一行熊猫系列-数据框
#2楼
You can think DataFrame as a dict of Series. 您可以将DataFrame视为Series的字典。 df[key]
try to select the column index by key
and returns a Series object. df[key]
尝试通过key
选择列索引并返回Series对象。
However slicing inside of [] slices the rows, because it's a very common operation. 但是,在[]内切片会对行进行切片,因为这是非常常见的操作。
You can read the document for detail: 您可以阅读文档以了解详细信息:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#basics http://pandas.pydata.org/pandas-docs/stable/indexing.html#basics
#3楼
You can take a look at the source code . 您可以看一下源代码 。
DataFrame
has a private function _slice()
to slice the DataFrame
, and it allows the parameter axis
to determine which axis to slice. DataFrame
具有专用函数_slice()
来切片DataFrame
,并且它允许参数axis
确定要切片的轴。 The __getitem__()
for DataFrame
doesn't set the axis while invoking _slice()
. 调用_slice()
时, DataFrame
的__getitem__()
不会设置轴。 So the _slice()
slice it by default axis 0. 因此_slice()
默认将其切片为轴0。
You can take a simple experiment, that might help you: 您可以进行一个简单的实验,这可能对您有所帮助:
print df._slice(slice(0, 2))
print df._slice(slice(0, 2), 0)
print df._slice(slice(0, 2), 1)
#4楼
echoing @HYRY, see the new docs in 0.11 回显@HYRY,请参阅0.11中的新文档
http://pandas.pydata.org/pandas-docs/stable/indexing.html http://pandas.pydata.org/pandas-docs/stable/indexing.html
Here we have new operators, .iloc
to explicity support only integer indexing, and .loc
to explicity support only label indexing 在这里,我们有了新的运算符, .iloc
显式仅支持整数索引, .loc
显式仅支持标签索引
eg imagine this scenario 例如,想象这种情况
In [1]: df = pd.DataFrame(np.random.rand(5,2),index=range(0,10,2),columns=list('AB'))In [2]: df
Out[2]: A B
0 1.068932 -0.794307
2 -0.470056 1.192211
4 -0.284561 0.756029
6 1.037563 -0.267820
8 -0.538478 -0.800654In [5]: df.iloc[[2]]
Out[5]: A B
4 -0.284561 0.756029In [6]: df.loc[[2]]
Out[6]: A B
2 -0.470056 1.192211
[]
slices the rows (by label location) only []
仅对行进行切片(按标签位置)
#5楼
you can loop through the data frame like this . 您可以像这样遍历数据帧。
for ad in range(1,dataframe_c.size):print(dataframe_c.values[ad])
#6楼
To index-based access to the pandas table, one can also consider numpy.as_array option to convert the table to Numpy array as 要基于索引访问熊猫表,还可以考虑使用numpy.as_array选项将表转换为Numpy数组,如下所示:
np_df = df.as_matrix()
and then 接着
np_df[i]
would work. 会工作。
通过整数索引选择一行熊猫系列/数据框相关推荐
- 两个选择框 ajax如何根据另一个选择框的内容获取_Python数据结构:数据框
前一篇文章提到了Python数据结构:神奇的序列,可以理解为Excel里没有列名的一列数据,那么Excel里的由行列组成的表数据是如何对应到Python中的呢?就是今天要说的数据框:DataFrame ...
- pandas数据框loc属性语法及示例
pandas.DataFrame.loc[] 是数据框的属性,通过指定标签或布尔数组来访问数据框的一组行和列.pandas.DataFrame是带有轴标签的二维表数据结构,如列和行.从数据框选择列结果 ...
- python 读取excel文件,并读成数据框格式输出
pandas直接读取 import pandas as pd df = pd.read_excel('/path/file.xlsx' ) sheet_name: str, int, list, or ...
- pandas 索引_Pandas学习笔记03数据清洗(通过索引选择数据)
点击上方"可以叫我才哥"关注我们 今天我们就在jupyterlab里进行操作演示,本次推文内容主要以截图为主了. 有兴趣的可以公众号回复 "索引" 获取 演示原 ...
- 在可编辑表格EditorGrid中,我选择一行已输入的数据,点击删除按钮,该行数据将被删除,然后当我点击表单提交按钮时,已经被删除的那一行数据仍然被插入数据库中...
为什么80%的码农都做不了架构师?>>> 问题描述:在可编辑表格中,我选择一行已输入的数据,点击删除按钮,该行数据将被删除,然后当我点击表单提交按钮时,已经被删除的那一行数据仍 ...
- python怎么索引txt数据中第四行_python-在熊猫数据框中按行计数编制索引
我有一个带有两个元素的层次结构索引的"熊猫"数据框(" month"和" item_id").每行表示特定月份的特定项目,并具有用于关注多个 ...
- [ES7版本系列(二)] Java连接ElasticSearch向索引中插入新的数据
1.引入依赖,这里使用的是es的7以上的版本,使用elasticsearch-rest-high-level-client 高级别API来创建客户端 <!-- https://mvnreposi ...
- 熊猫数据集_大熊猫数据框的5个基本操作
熊猫数据集 Tips and Tricks for Data Science 数据科学技巧与窍门 Pandas is a powerful and easy-to-use software libra ...
- 熊猫数据集_处理熊猫数据框中的列表值
熊猫数据集 Have you ever dealt with a dataset that required you to work with list values? If so, you will ...
最新文章
- PHP获取当前时间差8小时的问题
- 第一个java_第一个java程序
- php比较3个数大小写,php在前n个字符的字符串比较(大小写敏感)的函数strncmp()...
- python编程if语法-二、python 语法之变量赋值与if(if else)
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )
- Algorithms_基础数据结构(02)_线性表之链表_单向链表
- 从外网 SSH 进局域网,反向代理+正向代理解决方案
- java_函数的概述
- python的网络编程学什么_【Python学习】网络编程
- 在鉴定名画真伪这件事上,专家可能要被AI代替了
- 【笔试/面试】—— 从余弦定理到三角形两边之和大于第三边的证明
- 数据通信与网络有这一篇就够了(概述篇)
- 删除计算机网络无用设备,如何删除我的电脑/计算机中无效的设备和驱动器图标...
- 漫画追踪器Tracker
- 计算机unity文献综述,Unity3D密室逃脱游戏设计+文献综述.doc
- 超级经典回帖专用语(转载)
- 37岁,感觉上班太累,可做这5个生意,年轻人看不上,但却挺赚钱
- 34岁测试工程师面试美团遭拒:只招30岁以下,能加班但工资要求不高的....
- 如何查看支付宝商户ID(支付宝pid)?
- 有关如何用html制作电影影评网,初阶展示