2

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
  • 63,701
  • 20
  • 147
  • 175
o17t H1H' S'k
  • 2,541
  • 5
  • 31
  • 52

2 Answers2

4

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
  • 18,636
  • 3
  • 38
  • 64
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
  • 1,034
  • 1
  • 10
  • 16