How to generate list with out for loop in python

data frame

S/N Type Number Capacity 1 Bike 2 5 2 Tempo 1 30 3 Truck-1 1 60 4 Truck-2 1 90

I would like to generate capacitylist = [5,5,30,60,90]

Is it possible to do it with out for and using map function in python. Thanks Alot.

Topic map-reduce python

Category Data Science


ndf = df.loc[ np.repeat(df.index.values, df['Number']) ]
values = ndf['Capacity'].tolist()
# [5, 5, 30, 60, 90]

Repeat the rows you wish you duplicate using np.repeat, and then, takes the selected column (here, capacity) and turn it into a list using .tolist().

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.