The Python Oracle

Initialize empty Pandas series and conditionally add to it

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle

--

Chapters
00:00 Initialize Empty Pandas Series And Conditionally Add To It
00:46 Accepted Answer Score 14
01:01 Thank you

--

Full question
https://stackoverflow.com/questions/4870...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe

#avk47



ACCEPTED ANSWER

Score 14


If I understand correctly , you can using add + fill_value=0

equation = pd.Series([])
if keep['seasonality']:
    equation = equation.add(months,fill_value=0)
if keep['age']:
    equation = equation.add(age,fill_value=0)
equation
Out[91]: 
0    2.0
1    3.0
2    3.0
3    4.0
4    5.0
dtype: float64