python - Initialize dataframe with a constant -
initializing dataframe constant value not work,
pd.dataframe(0, index=[1,2,3]) # doesnt work! # or pd.dataframe(0) # doesnt work!
whereas observe that
(1) initializing series constant value works
pd.series(0, index=[1,2,3]) # works fine!
(2) initializing dataframe none works
pd.dataframe(none, index=[1,2,3]) # works fine!
(3) initializing dataframe when index , columns not provided works
pd.dataframe([1, 2, 3]) # works fine! pd.dataframe([0]) # works fine!
does know why?
i keen know more design consideration, rather answer "if check pandas code, you'd see fails 1 of checks data dimension expected >1.. blah blah".
i think should work intuitively (considering pandas intelligent in assigning default col , index when not provided, , guesses dimensions data given).
may there reason behaviour can't figure.
a pd.dataframe
2-dimensional. when specify
pd.dataframe(0, index=[1, 2, 3])
you telling constructor assign 0
every row indices 1
, 2
, , 3
. columns? didn't define columns.
you can 2 things
option 1
specify 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 list of values
pd.dataframe([[0]], index=[1, 2, 3]) 0 1 0 2 0 3 0
Comments
Post a Comment