通过整数索引选择一行熊猫系列/数据框

本文翻译自: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:00

In [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.800654

In [5]: df.iloc[[2]]
Out[5]: 
          A         B
4 -0.284561  0.756029

In [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. 会工作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值