Creating a zero-filled pandas data frame
What is the best way to create a zero-filled pandas data frame of a given size?
I have used:
zero_data = np.zeros(shape=(len(data),len(feature_list)))
d = pd.DataFrame(zero_data, columns=feature_list)
Is there a better way to do it?
You can try this:
d = pd.DataFrame(0, index=np.arange(len(data)), columns=feature_list)
It's best to do this with numpy in my opinion
import numpy as np
import pandas as pd
d = pd.DataFrame(np.zeros((N_rows, N_cols)))
If you would like the new data frame to have the same index and columns as an existing data frame, you can just multiply the existing data frame by zero:
df_zeros = df * 0
If the existing data frame contains NaNs or non-numeric values you can instead apply a function to each cell that will just return 0:
df_zeros = df.applymap(lambda x: 0)
Similar to @Shravan, but without the use of numpy:
height = 10
width = 20
df_0 = pd.DataFrame(0, index=range(height), columns=range(width))
Then you can do whatever you want with it:
post_instantiation_fcn = lambda x: str(x)
df_ready_for_whatever = df_0.applymap(post_instantiation_fcn)