Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Pandas - Delete the first row by group

Tags:

I have the following large dataframe (df) that looks like this:

    ID     date        PRICE        1   10001  19920103  14.500     2   10001  19920106  14.500     3   10001  19920107  14.500      4   10002  19920108  15.125      5   10002  19920109  14.500    6   10002  19920110  14.500     7   10003  19920113  14.500  8   10003  19920114  14.500      9   10003  19920115  15.000  

Question: What's the most efficient way to delete (or remove) the first row of each ID? I want this:

        ID     date     PRICE            2   10001  19920106  14.500         3   10001  19920107  14.500          5   10002  19920109  14.500        6   10002  19920110  14.500         8   10003  19920114  14.500          9   10003  19920115  15.000  

I can do a loop over each unique ID and remove the first row but I believe this is not very efficient.

like image 815
Plug4 Avatar asked Jul 05 '15 00:07

Plug4


People also ask

How do you delete the first row of a DataFrame in pandas?

Use iloc to drop first row of pandas dataframe. Use drop() to remove first row of pandas dataframe. Use tail() function to remove first row of pandas dataframe.

How do you get rid of the first 5 rows in pandas?

Remove First N Rows of Pandas DataFrame Using tail()tail(df. shape[0] -n) to remove the top/first n rows of pandas DataFrame. Generally, DataFrame. tail() function is used to show the last n rows of a pandas DataFrame but you can pass a negative value to skip the rows from the beginning.

How do you get rid of the first 3 rows in pandas?

To delete the first three rows of a DataFrame in Pandas, we can use the iloc() method.

How do I delete rows in pandas DataFrame based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).


1 Answers

Another one line code is df.groupby('ID').apply(lambda group: group.iloc[1:, 1:])

Out[100]:               date  PRICE ID                       10001 2  19920106   14.5       3  19920107   14.5 10002 5  19920109   14.5       6  19920110   14.5 10003 8  19920114   14.5       9  19920115   15.0 
like image 64
Jianxun Li Avatar answered Oct 12 '22 18:10

Jianxun Li