Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop all data in a pandas dataframe

I would like to drop all data in a pandas dataframe, but am getting TypeError: drop() takes at least 2 arguments (3 given). I essentially want a blank dataframe with just my columns headers.

import pandas as pd  web_stats = {'Day': [1, 2, 3, 4, 2, 6],              'Visitors': [43, 43, 34, 23, 43, 23],              'Bounce_Rate': [3, 2, 4, 3, 5, 5]} df = pd.DataFrame(web_stats)  df.drop(axis=0, inplace=True) print df 
like image 388
user2242044 Avatar asked Aug 26 '16 20:08

user2242044


People also ask

How delete all data from pandas DataFrame?

You can delete a list of rows from Pandas by passing the list of indices to the drop() method. In this code, [5,6] is the index of the rows you want to delete. axis=0 denotes that rows should be deleted from the dataframe.

How do you empty a Pandas series?

We can easily create an empty series in Pandas which means it will not have any value. The syntax that is used for creating an Empty Series: <series object> = pandas. Series()


2 Answers

You need to pass the labels to be dropped.

df.drop(df.index, inplace=True) 

By default, it operates on axis=0.

You can achieve the same with

df.iloc[0:0] 

which is much more efficient.

like image 50
ayhan Avatar answered Sep 21 '22 16:09

ayhan


My favorite:

df = df.iloc[0:0] 

But be aware df.index.max() will be nan. To add items I use:

df.loc[0 if math.isnan(df.index.max()) else df.index.max() + 1] = data 
like image 39
tomatom Avatar answered Sep 19 '22 16:09

tomatom