Python 通过排除索引号来选择熊猫行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28256761/
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
select pandas rows by excluding index number
提问by itjcms18
Not quite sure why I can't figure this out. I'm looking to slice a Pandas dataframe by using index numbers. I have a list/core index with the index numbers that i do NOT need, shown below
不太确定为什么我无法弄清楚这一点。我正在寻找使用索引号对 Pandas 数据框进行切片。我有一个列表/核心索引,其中包含我不需要的索引号,如下所示
pandas.core.index.Int64Index
Int64Index([2340, 4840, 3163, 1597, 491 , 5010, 911 , 3085, 5486, 5475, 1417, 2663, 4204, 156 , 5058, 1990, 3200, 1218, 3280, 793 , 824 , 3625, 1726, 1971, 2845, 4668, 2973, 3039, 376 , 4394, 3749, 1610, 3892, 2527, 324 , 5245, 696 , 1239, 4601, 3219, 5138, 4832, 4762, 1256, 4437, 2475, 3732, 4063, 1193], dtype=int64)
How can I create a new dataframe excluding these index numbers. I tried
如何创建不包括这些索引号的新数据框。我试过
df.iloc[combined_index]
and obviously this just shows the rows with those index number (the opposite of what I want). any help will be greatly appreciated
显然这只是显示具有这些索引号的行(与我想要的相反)。任何帮助将不胜感激
采纳答案by Vor
Not sure if that's what you are looking for, posting this as an answer, cause it's too long for a comment:
不确定这是否是您要查找的内容,将其作为答案发布,因为评论太长了:
In [31]: d = {'a':[1,2,3,4,5,6], 'b':[1,2,3,4,5,6]}
In [32]: df = pd.DataFrame(d)
In [33]: bad_df = df.index.isin([3,5])
In [34]: df[~bad_df]
Out[34]:
a b
0 1 1
1 2 2
2 3 3
4 5 5
回答by unutbu
You could use pd.Int64Index(np.arange(len(df))).difference(index)
to form a new ordinal index. For example, if we want to remove the rows associated with ordinal index [1,3,5], then
您可以使用pd.Int64Index(np.arange(len(df))).difference(index)
来形成新的序数索引。例如,如果我们要删除与序数索引 [1,3,5] 关联的行,则
import numpy as np
import pandas as pd
index = pd.Int64Index([1,3,5], dtype=np.int64)
df = pd.DataFrame(np.arange(6*2).reshape((6,2)), index=list('ABCDEF'))
# 0 1
# A 0 1
# B 2 3
# C 4 5
# D 6 7
# E 8 9
# F 10 11
new_index = pd.Int64Index(np.arange(len(df))).difference(index)
print(df.iloc[new_index])
yields
产量
0 1
A 0 1
C 4 5
E 8 9
回答by Allen Wang
Probably an easier way is just to use a boolean index, and slice normally doing something like this:
可能更简单的方法是使用布尔索引,然后切片通常执行以下操作:
df[~df.index.isin(list_to_exclude)]
回答by Chris Farr
Just use .drop
and pass it the index list to exclude.
只需使用.drop
并将其传递给要排除的索引列表即可。
import pandas as pd
df = pd.DataFrame({"a": [10, 11, 12, 13, 14, 15]})
df.drop([1, 2, 3], axis=0)
Which outputs this.
哪个输出这个。
a
0 10
4 14
5 15