The Python Oracle

Initialize empty Pandas series and conditionally add to it

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC D Schuberts Piano Sonata D 850 in D

--

Chapters
00:00 Question
01:01 Accepted answer (Score 13)
01:21 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