Skip to content Skip to sidebar Skip to footer

Initialize Dataframe With A Constant

Initializing dataframe with a constant value does not work, pd.DataFrame(0, index=[1,2,3]) # doesnt work! # OR pd.DataFrame(0) # doesnt work! whereas

Solution 1:

A pd.DataFrame is 2-dimensional. When you specify

pd.DataFrame(0, index=[1, 2, 3])

You are telling the constructor to assign 0 to every row with indices 1, 2, and 3. But what are the columns? You didn't define any columns.

You can do two things

Option 1
specify your columns

pd.DataFrame(0, index=[1, 2, 3], columns=['x', 'y'])

   x  y
1  0  0
2  0  0
3  0  0

Option 2
pass a list of values

pd.DataFrame([[0]], index=[1, 2, 3])

   0
1  0
2  0
3  0

Post a Comment for "Initialize Dataframe With A Constant"