How to create lists from pandas columns

Question:

I have created a pandas dataframe using this code:

import numpy as np
import pandas as pd

ds = {'col1': [1,2,3,3,3,6,7,8,9,10]}


df = pd.DataFrame(data=ds)

The dataframe looks like this:

print(df)

   col1
0     1
1     2
2     3
3     3
4     3
5     6
6     7
7     8
8     9
9    10

I need to create a field called col2 that contains in a list (for each record) the last 3 elements of col1 while iterating through each record. So, the resulting dataframe would look like this:

enter image description here

Does anyone know how to do it by any chance?

Asked By: Giampaolo Levorato

||

Answers:

Use a list comprehension:

N = 3
l = df['col1'].tolist()

df['col2'] = [l[max(0,i-N+1):i+1] for i in range(df.shape[0])]

Output:


   col1        col2
0     1         [1]
1     2      [1, 2]
2     3   [1, 2, 3]
3     3   [2, 3, 3]
4     3   [3, 3, 3]
5     6   [3, 3, 6]
6     7   [3, 6, 7]
7     8   [6, 7, 8]
8     9   [7, 8, 9]
9    10  [8, 9, 10]
Answered By: mozway

Here is a solution using rolling and list comprehension

df['col2'] = [x.tolist() for x in df['col1'].rolling(3)]

   col1        col2
0     1         [1]
1     2      [1, 2]
2     3   [1, 2, 3]
3     3   [2, 3, 3]
4     3   [3, 3, 3]
5     6   [3, 3, 6]
6     7   [3, 6, 7]
7     8   [6, 7, 8]
8     9   [7, 8, 9]
9    10  [8, 9, 10]
Answered By: It_is_Chris
lastThree = []
for x in range(len(df)):
    lastThree.append([df.iloc[x - 2]['col1'], df.iloc[x - 1]['col1'], df.iloc[x]['col1']])

df['col2'] = lastThree
Answered By: StackKaan

Upon seeing the other answers, I’m affirmed my answer is pretty stupid.
Anyways, here it is.

import pandas as pd
ds = {'col1': [1,2,3,3,3,6,7,8,9,10]}
df = pd.DataFrame(data=ds)
df['col2'] = df['col1'].shift(1)
df['col3'] = df['col2'].shift(1)
df['col4'] = (df[['col3','col2','col1']]
    .apply(lambda x:','.join(x.dropna().astype(str)),axis=1)
)

The last column contains the resulting list.

   col1          col4
0     1           1.0
1     2       1.0,2.0
2     3   1.0,2.0,3.0
3     3   2.0,3.0,3.0
4     3   3.0,3.0,3.0
5     6   3.0,3.0,6.0
6     7   3.0,6.0,7.0
7     8   6.0,7.0,8.0
8     9   7.0,8.0,9.0
9    10  8.0,9.0,10.0
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.