Following reproducible script is intended to process each two rows in a dataframe with length 5. For each two processed rows, I like to print a list of items that have been processed.
import pandas as pd
import itertools
my_dict = {
'name' : ['a', 'b', 'c', 'd', 'e'],
'age' : [10, 20, 30, 40, 50]
}
df = pd.DataFrame(my_dict)
for index, row in itertools.islice(df.iterrows(), 2):
rowlist = (row.name)
print('Processed two rows {}'.format(rowlist))
Output:
a
b
I'm looking for a way to get following desired output:
[a,b]
[c,d]
[e]
Tried:
print(df.groupby(df.index//2)['name'].agg(list))
Out:
0 [a, b]
1 [c, d]
2 [e]
Name: name, dtype: object
0 [a, b]
1 [c, d]
2 [e]
Name: name, dtype: object
Thanks for your help!