cammil
cammil

Reputation: 9937

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

Upvotes: 54

Views: 91544

Answers (6)

MAYANK PANDE
MAYANK PANDE

Reputation: 63

Old Thread. But i found another way

df_final=df_dup[0:0].copy(deep=True)

Upvotes: 0

rachwa
rachwa

Reputation: 2310

You can also use head:

df_empty = df.head(0)

Upvotes: 11

Emin Berkay Dağlar
Emin Berkay Dağlar

Reputation: 97

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

This line will delete all rows, while keeping the column names.

Upvotes: 6

Lelouch
Lelouch

Reputation: 579

If you have an existing DataFrame with the columns you want then extract the column names into a list comprehension then create an empty DataFrame with your column names.

# Creating DataFrame from a CSV file with desired headers
csv_a = "path/to/my.csv"
df_a = pd.read_csv(csv_a)

# Extract column names into a list
names = [x for x in df_a.columns]

# Create empty DataFrame with those column names
df_b = pd.DataFrame(columns=names)

Upvotes: 6

ashishsingal
ashishsingal

Reputation: 2978

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

Upvotes: 118

FooBar
FooBar

Reputation: 16528

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

Upvotes: 7

Related Questions