I have a dataframe df :
JavaScript
x
10
10
1
>>> df
2
sales discount net_sales cogs
3
STK_ID RPT_Date
4
600141 20060331 2.709 NaN 2.709 2.245
5
20060630 6.590 NaN 6.590 5.291
6
20060930 10.103 NaN 10.103 7.981
7
20061231 15.915 NaN 15.915 12.686
8
20070331 3.196 NaN 3.196 2.710
9
20070630 7.907 NaN 7.907 6.459
10
Then I want to drop rows with certain sequence numbers which indicated in a list, suppose here is [1,2,4],
then left:
JavaScript
1
6
1
sales discount net_sales cogs
2
STK_ID RPT_Date
3
600141 20060331 2.709 NaN 2.709 2.245
4
20061231 15.915 NaN 15.915 12.686
5
20070630 7.907 NaN 7.907 6.459
6
How or what function can do that ?
Advertisement
Answer
Use DataFrame.drop and pass it a Series of index labels:
JavaScript
1
15
15
1
In [65]: df
2
Out[65]:
3
one two
4
one 1 4
5
two 2 3
6
three 3 2
7
four 4 1
8
9
10
In [66]: df.drop(index=[1,3])
11
Out[66]:
12
one two
13
one 1 4
14
three 3 2
15