Home > Software engineering >  How do I get this for loop to print a year for the amount of times of a value in another column
How do I get this for loop to print a year for the amount of times of a value in another column

Time:02-18

So I have a column release.TOTAL with values like [38,24,44,58,50,..]. This column states how many major films were made in a given year. What I want is for this to make a list that lists the year for each of the values. For example, if there were 25 movies made in 2016 there would be 25 2016s in the list.

total_years = [] 
for i in release.TOTAL:
        for j in range(i):
            for k in release.YEAR:
                total_years.append(k)

This is the function I have now but its printing the entire column each time the for loop runs. So how can I edit it so it does what I want.

CodePudding user response:

If i understand release is dataframe that has two columns YEAR,TOTAL

def append_years(yr,val,list_in):
    for i in range(val):
        list_in.append(yr)
    return list_in

total_years = [] 
for i in range(len(release)):
    total_years=append_years(release.YEAR[i],release.TOTAL[i],total_years)
print(total_years)

CodePudding user response:

I think you want something like this?

class release:
    YEAR = null
    TOTAL = list()

release.YEAR = "2016"
release.TOTAL = [38,24,44,58,50]

total_years = [] 
for i in range( len(release.TOTAL)):
    total_years.append(release.YEAR)

print (total_years)
  • Related