根据列表索引选择 pandas 行 [英] Select Pandas rows based on list index

查看:48
本文介绍了根据列表索引选择 pandas 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df:

   20060930  10.103       NaN     10.103   7.981
   20061231  15.915       NaN     15.915  12.686
   20070331   3.196       NaN      3.196   2.710
   20070630   7.907       NaN      7.907   6.459

然后我要选择具有某些序列号的行,这些行号在列表中指示,假设这里是[1,3],然后向左移:

Then I want to select rows with certain sequence numbers which indicated in a list, suppose here is [1,3], then left:

   20061231  15.915       NaN     15.915  12.686
   20070630   7.907       NaN      7.907   6.459

如何或什么功能可以做到这一点?

How or what function can do that ?

推荐答案

List = [1, 3]
df.ix[List]

应该可以解决问题! 当我用数据帧建立索引时,我总是使用.ix()方法.它是如此的容易和灵活...

should do the trick! When I index with data frames I always use the .ix() method. Its so much easier and more flexible...

更新 这不再是公认的索引编制方法. ix方法已被弃用.将.iloc用于基于整数的索引,将.loc用于基于标签的索引.

UPDATE This is no longer the accepted method for indexing. The ix method is deprecated. Use .iloc for integer based indexing and .loc for label based indexing.

这篇关于根据列表索引选择 pandas 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆