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 avoid fragmentation warning when moving a column to position 0?

single statement of

df.insert(0, 'time', df.pop('time'))

on large dataframe causing

warning: PerformanceWarning: DataFrame is highly fragmented.

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

is there away to move a column to position ZERO to avoid this warning.

>Solution :

Consider this dataframe:

   column1  column2  time
0        1        2     3
1        4        5     6

You can reorder the columns and put the time column first like this:

df = df[["time", *df.columns.difference(["time"])]]
print(df)

Prints:

   time  column1  column2
0     3        1        2
1     6        4        5
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