'pythonic way to get a zero-record slice of a pandas dataframe
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?
Solution 1:[1]
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
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | machow |
