1

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.

1 Answer 1

3

Use tolist() to convert the values you need to store in column b to a list of lists, and then assign directly; this should be more efficient than creating column b on the go:

df = pd.DataFrame({'a': [1,2,3,4,5,6,7,8,9]})

for days in [2,4]:
    labelMA  = "MA" + str(days)
    df[labelMA] = df["a"].rolling(window =  days, center = False).mean() 

df['b'] = df.values.tolist()

df
#   a   MA2 MA4               b
#0  1   NaN NaN [1.0, nan, nan]
#1  2   1.5 NaN [2.0, 1.5, nan]
#2  3   2.5 NaN [3.0, 2.5, nan]
#3  4   3.5 2.5 [4.0, 3.5, 2.5]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.