How to delete all rows in a data frame?

I want to delete all rows in a data frame.

The reason I want to do this is because I can restore the data file using an iterative loop. I want to start with a completely empty framework.

Alternatively, I could only create an empty df from the column / type information, if possible

+6
source share
2 answers

The latter is possible and highly recommended - "inserting" lines by lines is highly inefficient. Sketch may be

>>> import numpy as np >>> import pandas as pd >>> index = np.arange(0, 10) >>> df = pd.DataFrame(index=index, columns=['foo', 'bar']) >>> df Out[268]: foo bar 0 NaN NaN 1 NaN NaN 2 NaN NaN 3 NaN NaN 4 NaN NaN 5 NaN NaN 6 NaN NaN 7 NaN NaN 8 NaN NaN 9 NaN NaN 
+1
source

Here's another method if you have an existing DataFrame that you want to clear without re-creating the column information:

 df_empty = df[0:0] 

df_empty is a DataFrame with zero rows, but with the same column structure as df

+10
source

Source: https://habr.com/ru/post/971867/


All Articles