【发布时间】:2020-01-01 14:23:25
【问题描述】:
类似于Select multiple sections of rows by index in pandas,我有很大的数据框,有一些我有兴趣查看的索引。
我有:
import pandas as pd
df = pd.DataFrame({'A':[0,1,2,3,4,5,6,7,8,9],'B':['a','b','c','d','e','f','g','h','i','j']},
index=range(10,20,))
我想看的索引很少,list=[12,15,10,14]
所以我最终得到:
A B
12 2 c
15 5 f
10 0 a
14 4 e
有没有命令让我可以去:
df.iloc[[index isin list]]
因为值列表是由较早的一段代码组成的。
我试过了:
df.loc[[ix]]
ix=dm.iloc[250].sort_values()[:10].index 是Int64Index([250, 1109, 427, 513, 678, 18, 697, 1075, 533, 739], dtype='int64')
无济于事。
欢迎提出建议!
【问题讨论】:
-
df.loc[list_]也不要将变量命名为list,因为它是一个内置函数