I'm trying to create a dataframe where one column is a list of the values of other columns, something like:
a b MA2 MA4
0 1 [1,NaN,NaN] NaN Nan
1 2 [2,1.5,NaN] 1.5 NaN
2 3 [3,2.5,NaN] 2.5 NaN
3 4 [4,3.5,2.5] 3.5 2.5
...
but I can't figure out how to make the values in column b a list.
My test code is:
df = pd.DataFrame({'a': [1,2,3,4,5,6,7,8,9]})
df["b"] = list(df["a"])
for days in [2,4]:
labelMA = "MA" + str(days)
df[labelMA] = df["a"].rolling(window = days, center = False).mean()
df["b"] += list(df[labelMA])
but this (and other variations I've tried) produce a single-valued b column.