Ian
Ian

Reputation: 1707

How to store list in python dataframe

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.

Upvotes: 1

Views: 4308

Answers (1)

akuiper
akuiper

Reputation: 214927

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]

Upvotes: 3

Related Questions