Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

python - select pandas rows by excluding index number

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.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not sure if that's what you are looking for, posting this as an answer, because 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...