3

I have a pandas data frame, and I want to get a zero-record slice. That is, a dataframe with the same columns but zero rows. The reason I am doing this, is because i want to have an empty dataframe, to which i add rows from the original dataframe in a loop.

Currently if am using:

empty = df[0:0]

is this the pythonic way?

Lev Levitsky
  • 59,844
  • 20
  • 139
  • 166
o17t H1H' S'k
  • 2,191
  • 5
  • 27
  • 47

2 Answers2

5

Well, obvious way to make dataframe with known columns is to do

import pandas as pd
df = pd.DataFrame(columns=["A", "B", "C"])

You'll get empty dataframe as desired. But adding rows one by one is NOT most efficient way of operations

UPDATE

There was a discussion quite some time ago, take a look at add one row in a pandas.DataFrame

Community
  • 1
  • 1
Severin Pappadeux
  • 16,848
  • 3
  • 34
  • 60
1

You can get a zero-record slice by indexing with something that returns no rows:

import pandas as pd

df = pd.DataFrame({"x": [1,2], "y": [1.2, 3.4]})

# select rows using an empty index, so get no rows back
res = df.loc[pd.Index([]), :]

Here's the result:

Empty DataFrame
Columns: [x]
Index: []

The accepted answer does not necessarily give back a zero-record slice of the original DataFrame. Its dtypes will all be object. This is not the case with the approach above!

We can check its dtypes to verify:

res.dtypes

Gives:

x      int64
y    float64
dtype: object
machow
  • 992
  • 1
  • 8
  • 16