How to delete all rows in a dataframe?

I want to delete all the rows in a dataframe.

The reason I want to do this is so that I can reconstruct the dataframe with an iterative loop. I want to start with a completely empty dataframe.

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

Answers


The latter is possible and strongly recommended - "inserting" rows row-by-row is highly inefficient. A sketch could 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

Here's another method if you have an existing DataFrame that you'd like to empty without recreating the column information:

df_empty = df[0:0]

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


Need Your Help

Choose one of many Internet connections for an application

c# network-programming

I have a computer with a few different internet connections. LAN, WLAN, WiFi or 3G. All of these are active and the machine can use any of them.

How to use __doPostBack()

c# javascript asp.net postback

I'm trying to create an asyncrhonous postback in ASP.NET using __doPostBack(), but I have no idea how to do it. I want to use vanilla JavaScript.