Reputation: 755
How can I create an empty dataframe in python such that the test df.empty
results True
?
I tried this:
df = pd.DataFrame(np.empty((1, 1)))
and df.empty
results in False
.
Upvotes: 17
Views: 36777
Reputation: 214927
The simplest is pd.DataFrame()
:
df = pd.DataFrame()
df.empty
# True
If you want create a data frame of specify number of columns:
df = pd.DataFrame(columns=['A', 'B'])
df.empty
# True
Besides, an array of shape (1, 1) is not empty (it has one row), which is the reason you get empty = False, in order to create an empty array, it needs to be of shape (0, n):
df = pd.DataFrame(pd.np.empty((0, 3)))
df.empty
# True
Upvotes: 39