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 1specify your columns

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

   xy100200300

Option 2pass a list of values

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

   0102030

Post a Comment for "Initialize Dataframe With A Constant"