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.
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:])df=df.iloc[3:9]?pd.concat(). Something like, df2 = pd.concat([df.iloc[:3],df.iloc[10:]]).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)
drop can even be calculated in-place (without extra assignment). Faster and simpler!df.drop(label, inplace=True)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
pandas?inplace parameter that shows up in many methods in pandas.truncateTo 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)
There is a simple way to implement that by the drop command.
df = df.drop(3)
header=3constructor argument which will set that row as the header row: stackoverflow.com/a/51822697/191246