I have defined a list as: name=[1, 5, 8, 10, 11, 20]. How to remove the rows from a dataframe specific to Index Labels for the numbers given in the list?
Asked
Active
Viewed 40 times
1 Answers
0
You can remove rows from a data frame specific to Index by using drop
. That can accept Index as a named list, like this:
Note: ‘ignore’, suppress error and only existing labels are dropped.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(100).reshape(25, 4), columns=['A', 'B', 'C', 'D'])
name=[1, 5, 8, 10, 11, 20]
df=df.drop(name, errors='ignore')
print(df)
Output:
A B C D
0 0 1 2 3
2 8 9 10 11
3 12 13 14 15
4 16 17 18 19
6 24 25 26 27
7 28 29 30 31
9 36 37 38 39
12 48 49 50 51
13 52 53 54 55
14 56 57 58 59
15 60 61 62 63
16 64 65 66 67
17 68 69 70 71
18 72 73 74 75
19 76 77 78 79
21 84 85 86 87
22 88 89 90 91
23 92 93 94 95
24 96 97 98 99

AziMez
- 2,014
- 1
- 6
- 16