pythonpython-2.7pandas

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

Solution

  • 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.