Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete every 0.2-th row in a pandas dataframe?

df_o = pd.DataFrame(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'])

I would like to continuously delete 4 consecutive rows but always spare the fifth in the dataframe above. The final result should be:

df_o = pd.DataFrame(['e', 'j', 'o'])

My idea df_o = df_o.drop(df_o.iloc[::0.2].index)does not work. It works well for deleting every n-th row if n is an integer, but not for my case.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

df_o.groupby(np.arange(len(df_o.index))//5).last()

Output:

   0
0  e
1  j
2  o
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading