A dataframe is being created by a process and sometimes the process returns an empty dataframe with no values at all. In that case I want the dataframe to be filled with zeroes for all the columns. I've tried output_df.fillna(value=0, inplace=True)
but it doesn't work. The dataframe just remains empty.
CodePudding user response:
To replace all values with 0, you can use:
df.loc[:] = 0
If you really have no rows and want to add one:
df.loc[0] = 0
CodePudding user response:
If has empty DataFrame only with columns names possible solution for add 0
is use DataFrame.reindex
by some list for new indices:
df = pd.DataFrame(columns=['a', 'b'])
df = df.reindex([0, 1, 2], fill_value=0)
print (df)
a b
0 0 0
1 0 0
2 0 0
df = pd.DataFrame()
df = df.reindex(index=[0, 1, 2], columns=list('ab'), fill_value=0)
print (df)
a b
0 0.0 0.0
1 0.0 0.0
2 0.0 0.0