Search code examples
pythonfor-loopnested-loops

Saving result from for loop to different columns


I am trying to run a nested loop in which I want the output to be saved in four different columns. Let C1R1 be the value I want in the first column first row, C2R2 the one I want in the second column second row, etc. What I have come up with this far gives me a list where the output is saved like this: ['C1R1', 'C2R1', 'C3R1', 'C4R1']. This is the code I am using:

dfs1 = []
for i in range(24):
    pd = (data_json2['data']['Rows'][i])
    for j in range(4):
        pd1 = pd['Columns'][j]['Value']
        dfs1.append(pd1)

What could be a good way to achieve this?

EDIT: This is what I want to achieve:

    Column 1  Column 2  Column 3  Column 4
0          0        24        48        72
1          1        25        49        73
2          2        26        50        74
3          3        27        51        75
4          4        28        52        76
5          5        29        53        77
6          6        30        54        78
7          7        31        55        79
8          8        32        56        80
9          9        33        57        81
10        10        34        58        82
11        11        35        59        83
12        12        36        60        84
13        13        37        61        85
14        14        38        62        86
15        15        39        63        87
16        16        40        64        88
17        17        41        65        89
18        18        42        66        90
19        19        43        67        91
20        20        44        68        92
21        21        45        69        93
22        22        46        70        94
23        23        47        71        95

While this is what I got now:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]

Thank you.


Solution

  • Try:

    import pandas as pd
    
    
    def get_dataframe(num_cols=4, num_values=24):
        return pd.DataFrame(
            ([v * 24 + c for v in range(num_cols)] for c in range(num_values)),
            columns=[f"Column {c}" for c in range(1, num_cols + 1)],
        )
    
    
    df = get_dataframe()
    print(df)
    

    Prints:

        Column 1  Column 2  Column 3  Column 4
    0          0        24        48        72
    1          1        25        49        73
    2          2        26        50        74
    3          3        27        51        75
    4          4        28        52        76
    5          5        29        53        77
    6          6        30        54        78
    7          7        31        55        79
    8          8        32        56        80
    9          9        33        57        81
    10        10        34        58        82
    11        11        35        59        83
    12        12        36        60        84
    13        13        37        61        85
    14        14        38        62        86
    15        15        39        63        87
    16        16        40        64        88
    17        17        41        65        89
    18        18        42        66        90
    19        19        43        67        91
    20        20        44        68        92
    21        21        45        69        93
    22        22        46        70        94
    23        23        47        71        95