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
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.
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()
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With