简体   繁体   中英

Pandas drop first n Rows per column id

Example DataFrame

df = pd.DataFrame(np.random.randint(2200, 3300, 50), index=[np.random.randint(0,6, 50)] ,columns=list('A'))

Below is a sample of what the data would look like

    A
5  2393
4  2421
0  3038
5  2914
4  2559
4  2314
5  3006
3  2553
0  2642
3  2441
3  2512
0  2412

What I would like to do is drop the first n (lets use 2 for this example) records of index. So from the previous data example it would become...

    A
4  2314
5  3006
3  2512
0  2412

Any guidance here would be appreciated. I haven't been able to get anything to work.

use tail with -2

s.groupby(level=0, group_keys=False).apply(pd.DataFrame.tail, n=-2)

      A
0  2412
3  2512
4  2314
5  3006

To really nail it down

s.groupby(level=0, group_keys=False, sort=False).apply(pd.DataFrame.tail, n=-2)

      A
5  3006
4  2314
0  2412
3  2512

If I understand your question, you want a new dataframe that has the first n rows of your dataframe removed. If that's what you want, I would reset the index, then drop based on pandas' default index, then put the original index back. Here's how you might do that.

df = pd.DataFrame(data=np.random.randint(2200, 3300, 50), 
                  index=np.random.randint(0,6, 50),
                  columns=list('A'))
n = 5
print(df.head(n * 2))
df_new = df.reset_index().drop(range(n)).set_index('index')
print(df_new.head(n))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM