1

I have a for loop that does calculations from multiple columns in a dataframe with multiple criteria that prints float values I need to arrange in a table.

   demolist = ['P13+', 'P18-34']
   impcount = ['<1M', '1-5M']
   for imp in impcount:
        print(imp)
        for d in demolist:
            print(d)
            target_ua = df.loc[(df['target'] == d) & (df['IMP Count'] == imp), 'in_target_ua_digital'].sum()
            target_pop = df.loc[(df['target'] == d) & (df['IMP Count'] == imp), 'in_target_pop'].sum()

            target_reach = target_ua / target_pop
            print(target_reach)

The output looks like this:

<1M
P13+
0.10
P18-34
0.12
1-5M
P13+
0.92
P18-34
0.53

The code is working correctly, but I need the output to be arranged in a new dataframe with impcount in the columns and demolist in the rows

         <1M        1-5M
P13+     0.10       0.92
P18-34   0.12       0.53

2 Answers 2

2

It is just a matter of how to arrange your data. A table is a 2D data structure, which is often represented as a list of list (tuple) in python, e.g. [[1,2], [3, 4]]. For your case, you could collect your data row by row to build the table data, meaning that generate a tuple or list for each element of the row, then for the whole row we get a list of list (the table).

Here is an example showing how to form a table when each value of each cell could be calculated (here is a random value)

In [53]: x = list('abc')
    ...: y = list('123')
    ...: 
    ...: data=[]
    ...: for i in x:
    ...:     row=[]
    ...:     for j in y:
    ...:         row.append(np.random.rand())
    ...:     data.append(row)
    ...: 
    ...: df = pd.DataFrame(data, index=x, columns=y)
    ...: 

In [54]: df
Out[54]: 
          1         2         3
a  0.107659  0.840387  0.642285
b  0.184508  0.641443  0.475105
c  0.503608  0.379945  0.933735
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

demolist = ['P13+', 'P18-34']
impcount = ['<1M', '1-5M']

imp_str = '\t'
for imp in impcount:
    imp_str += imp + '\t'
print(imp_str.rstrip())

imp_counter = 0
for imp in impcount:
    demo_str = demolist[imp_counter]+'\t'
    for d in demolist:
        target_ua = df.loc[(df['target'] == d) & (df['IMP Count'] == imp), 'in_target_ua_digital'].sum()
        target_pop = df.loc[(df['target'] == d) & (df['IMP Count'] == imp), 'in_target_pop'].sum()
        target_reach = target_ua / target_pop
        demo_str += str(target_reach)+'\t'
    print(demo_str.rstrip())
    imp_counter += 1

Hope this helps!

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.