Python 根据列表索引选择 Pandas 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19155718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:00:53  来源:igfitidea点击:

Select Pandas rows based on list index

pythonpandas

提问by user2806761

I have a dataframe df :

我有一个数据框 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

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

然后我想选择具有列表中指示的某些序列号的行,假设这里是 [1,3],然后离开:

   20061231  15.915       NaN     15.915  12.686
   20070630   7.907       NaN      7.907   6.459

How or what function can do that ?

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

回答by Woody Pride

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

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

应该做的伎俩!当我用数据框索引时,我总是使用 .ix() 方法。它更容易,更灵活......

UPDATEThis is no longer the accepted method for indexing. The ixmethod is deprecated. Use .ilocfor integer based indexing and .locfor label based indexing.

更新这不再是公认的索引方法。该ix方法已弃用。使用.iloc基于整数索引和.loc基于标签索引。

回答by yemu

you can also use iloc:

您还可以使用 iloc:

df.iloc[[1,3],:]

This will not work if the indexes in your dataframe do not correspond to the order of the rows due to prior computations. In that case use:

如果由于先前的计算,数据框中的索引与行的顺序不对应,这将不起作用。在这种情况下使用:

df.index.isin([1,3])

... as suggested in other responses.

...正如其他回复中所建议的那样。

回答by pylang

For large datasets, it is memory efficient to read only selected rows via the skiprowsparameter.

对于大型数据集,通过skiprows参数仅读取选定的行可以节省内存。

Example

例子

pred = lambda x: x not in [1, 3]
pd.read_csv("data.csv", skiprows=pred, index_col=0, names=...)

This will now return a DataFrame from a file that skips all rows except 1 and 3.

这现在将从跳过除 1 和 3 之外的所有行的文件中返回一个 DataFrame。



Details

细节

From the docs:

文档

skiprows: list-like or integer or callable, default None

...

If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise. An example of a valid callable argument would be lambda x: x in [0, 2]

skiprows: 类似列表或整数或可调用,默认 None

...

如果可调用,则可调用函数将根据行索引进行评估,如果应跳过该行则返回 True,否则返回 False。一个有效的可调用参数的例子是lambda x: x in [0, 2]

This feature works in version pandas 0.20.0+. See also the corresponding issueand a related post.

此功能适用于 pandas 0.20.0+ 版本。另请参阅相应问题相关帖子

回答by Amruth Lakkavaram

Another way (although it is a longer code) but it is faster than the above codes. Check it using %timeit function:

另一种方式(虽然它是一个更长的代码)但它比上面的代码更快。使用 %timeit 函数检查它:

df[df.index.isin([1,3])]

PS: You figure out the reason

PS:你找出原因

enter image description here

在此处输入图片说明