276

I need to delete the first three rows of a dataframe in pandas.

I know df.ix[:-1] would remove the last row, but I can't figure out how to remove first n rows.

JJJ
  • 1,009
  • 6
  • 19
  • 31
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121
  • For read_csv/read_html you can use e.g. `header=3` constructor argument which will set that row as the header row: https://stackoverflow.com/a/51822697/191246 – ccpizza Dec 16 '19 at 17:04

9 Answers9

437

Use iloc:

df = df.iloc[3:]

will give you a new df without the first three rows.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
bdiamante
  • 15,980
  • 6
  • 40
  • 46
  • 1
    doesn't that remove the first 4 rows instead the first 3 rows in the original question? – tagoma Nov 17 '16 at 18:16
  • 12
    No, It doesn't. The start position of the slice is always included. – bdiamante Jan 20 '17 at 19:30
  • Anyone happen to know how to do this in a `groupby()`? This works but returns duplicate columns in the index `df=pd.DataFrame({'v':np.arange(10).tolist()*2,'g':['a']*10+['b']*10});df.groupby('g').apply(lambda x: x.iloc[3:])` – citynorman Aug 06 '17 at 22:24
  • So if you want to delete from row 3 to row 9, for example, how would you do it? `df=df.iloc[3:9]`? – M.K Jun 26 '19 at 14:25
  • 1
    @M.K if using this approach, you can use this in combination with `pd.concat()`. Something like, `df2 = pd.concat([df.iloc[:3],df.iloc[10:]])`. – bdiamante Jun 26 '19 at 17:00
  • If you do this first row will be 3, how can I make it so it starts from 0. – haneulkim Jul 10 '19 at 04:24
  • @makewhite you can use `df = df.iloc[3:].reset_index(drop=True)` to reset the index to 0. – bdiamante Jul 10 '19 at 15:58
131

I think a more explicit way of doing this is to use drop.

The syntax is:

df.drop(label)

And as pointed out by @tim and @ChaimG, this can be done in-place:

df.drop(label, inplace=True)

One way of implementing this could be:

df.drop(df.index[:3], inplace=True)

And another "in place" use:

df.drop(df.head(3).index, inplace=True)
C Mars
  • 2,909
  • 4
  • 21
  • 30
  • 5
    `drop` can even be calculated in-place (without extra assignment). Faster and simpler! – tim Jun 13 '14 at 18:24
  • 1
    To expand on Tim's idea, Example: `df.drop(label, inplace=True)` – ChaimG Aug 26 '15 at 04:41
  • Due to index 0, I believe the implementation suggestion will delete 4 rows. – Daniel Morgan May 07 '16 at 18:34
  • 1
    @DanielMorgan That is not the case as python ranges are half open. As to why that is, is another question. See http://stackoverflow.com/questions/4504662/why-does-rangestart-end-not-include-end or https://www.quora.com/Why-are-Python-ranges-half-open-exclusive-instead-of-closed-inclusive – C Mars May 09 '16 at 07:10
  • 2
    @tim, according to [this](https://stackoverflow.com/questions/22532302/pandas-peculiar-performance-drop-for-inplace-rename-after-dropna/22533110#22533110), `inplace` operations are not faster. Also, simpler is a matter of opinion: I find it easier to read when the code doesn't have `inplace` parameters. – toto_tico Jul 26 '17 at 09:14
  • @toto_tico Afaik, inplace operations are ideally intended to avoid duplication in memory. That can be a big deal for a large object. Even if an old implementation uses a copy, an ideal implementation can avoid a copy. – Asclepius Oct 17 '18 at 01:04
16
df = df.iloc[n:]

n drops the first n rows.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
176coding
  • 2,933
  • 4
  • 17
  • 18
12

A simple way is to use tail(-n) to remove the first n rows

df=df.tail(-3)

mxia
  • 449
  • 5
  • 4
8
df.drop(df.index[[0,2]])

Pandas uses zero based numbering, so 0 is the first row, 1 is the second row and 2 is the third row.

Steffen Winkler
  • 2,805
  • 2
  • 35
  • 58
Anupam khare
  • 99
  • 1
  • 3
7

You can use python slicing, but note it's not in-place.

In [15]: import pandas as pd
In [16]: import numpy as np
In [17]: df = pd.DataFrame(np.random.random((5,2)))
In [18]: df
Out[18]:
          0         1
0  0.294077  0.229471
1  0.949007  0.790340
2  0.039961  0.720277
3  0.401468  0.803777
4  0.539951  0.763267

In [19]: df[3:]
Out[19]:
          0         1
3  0.401468  0.803777
4  0.539951  0.763267
beardc
  • 20,283
  • 17
  • 76
  • 94
5

inp0= pd.read_csv("bank_marketing_updated_v1.csv",skiprows=2)

or if you want to do in existing dataframe

simply do following command

Rahul kuchhadia
  • 289
  • 4
  • 10
0

Flexible truncation in pandas with truncate

To remove the first N rows

df.truncate(before=N)

To remove the last M rows

df.truncate(after=M)

To remove the first N and last M rows together

df.truncate(before=N, after=M)

To remove the first N columns

df.truncate(before=N, axis=1)
cs95
  • 379,657
  • 97
  • 704
  • 746
-1

There is a simple way to implement that by the drop command.

df = df.drop(3)
Flair
  • 2,609
  • 1
  • 29
  • 41
  • Hi, Aman. Please wrap code examples in code blocks for clarity ([see guidelines](https://stackoverflow.com/editing-help#code)). It also helps to reference documentation pages to help the questioner understand the method. – fpersyn Aug 23 '21 at 16:01
  • 3
    this will fail if your index is not 1, 2, 3, 4, 5, for example. – Mustafa Mar 19 '22 at 02:05