0

I need to loop a computation over two lists of elements and save the results in a table. So, say that

months = [1,2,3,4,5]
Region = ['Region1', 'Region2']

and that my code is of the type

df=[]
for month in month:
    for region in Region:
        code 
        x = result
        df.append(x)

What I cannot achieve is rendering the final result in a table in which the rows are regions and coumns are months

                 1            2          3          4          5

    Region1      a            b          c          d          e
    
    Region2      f            g          h          i          j
3
  • What are the variables code and result in your code ? In for month in month: the second month should have an s at the end. Commented Sep 24, 2020 at 12:45
  • Sorry for missing the s. As for code, it is not a variable (I didn't want to write all the steps of my computation. So code is the what happens within the loop. result is the value obtained at the end of each loop. Commented Sep 24, 2020 at 12:54
  • And the a-j are the results of each run. Commented Sep 24, 2020 at 13:02

3 Answers 3

2

Assuming that there is the right numbers of items in result

result = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
months = [1, 2, 3, 4, 5]
Region = ['Region1', 'Region2']
df = pd.DataFrame([[Region[i]] + result[i*len(months): ((i+1)*len(months))] for i in range(len(Region))], columns=["Region"] + months).set_index("Region")

Output

         1  2  3  4  5
Region                
Region1  a  b  c  d  e
Region2  f  g  h  i  j

This part

[[Region[i]] + result[i*len(months): ((i+1)*len(months))] for i in range(len(Region))]

is equivalent to something like this

res = []
for i in range(len(Region)):
    row = [Region[i]] + result[i*len(months): ((i+1)*len(months))]
    res.append(row)

where I use the length of Region to slice result in equals part for each row. And I add the name of the region at the begging of the row.

Sign up to request clarification or add additional context in comments.

2 Comments

This is great, but I still can't make it populate the dataframe with the results result = ['a','b','c','d','e','f','g']
This is brilliant!! Thank you!!
1

Another solution - more lines of code:

import pandas as pd
import ast
months = [1,2,3,4,5]
Regions = ['Region1', 'Region2']
       
df = pd.DataFrame()
for region in Regions:
    row = '{\'Region\': \'' +  region +'\', '
    for month in months:
        # put your calculation code
        x = month + 1
        row = row + '\'' + str(month) + '\':[' + str(x) + '],'
    row = row[:len(row)-1] + '}'
    row = ast.literal_eval(row)
    df = df.append(pd.DataFrame(row))
df

Comments

0

This might work, depending on what and how y want the results.

import pandas as pd
months = [1, 2, 3, 4, 5]
Region = ['Region1', 'Region2']

df = pd.DataFrame(columns=[1, 2, 3])  # just to put in something
value = 59
for r in Region:
    value += 5
    for m in months:
        df.loc[r, m] = chr(m+value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.