Home > Back-end >  How do I merge cells in Pandas Python?
How do I merge cells in Pandas Python?

Time:12-11

There is this data:

d1 = {"URL": ["https://example.com/1540"], "data": [2.5, 12.0, 5.0]}
d2 = {"URL": ["https://example.com/4541"], "data": [1.0, 8.0, 0.0]}

The main question is how do I combine it so that in excel it's like this: enter image description here

CodePudding user response:

Your advice helped, but not completely, I decided to add more data and this happens: OUTPUT IMAGE

In general, I want to get a result like this (I've already corrected it myself): FIX OUTPUT IMAGE

Here is a sample code snippet of how this happens in a function:

data = [
    {
        '1': [3.7],
        'X': [3.15],
        '2': [2.26],
        'ТОТАЛЫ': [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.5, 5, 5.5],
        'БОЛЬШЕ': [1.1, 1.12, 1.14, 1.29, 1.46, 1.6, 1.85, 2.16, 2.43, 2.89, 3.7, 4.1, 4.55, 5.7, 8.6, 9.3, 14.0, 14.0],
        'МЕНЬШЕ': [6.6, 6.0, 5.45, 3.4, 2.66, 2.34, 2.03, 1.71, 1.54, 1.39, 1.25, 1.21, 1.19, 1.13, 1.06, 1.05, 1.01, 1.01],
        'URL': ['https://www.flashscorekz.com/match/G0rn6WE5/#/match-summary/match-summary']
    },
    {
        '1': [2.8],
        'X': [3.15],
        '2': [2.8],
        'ТОТАЛЫ': [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.5, 5, 5.5],
        'БОЛЬШЕ': [1.09, 1.1, 1.12, 1.27, 1.42, 1.53, 1.74, 2.06, 2.34, 2.73, 3.45, 3.8, 4.1, 5.45, 7.5, 7.0, 11.0, 12.0],
        'МЕНЬШЕ': [7.0, 6.6, 6.0, 3.55, 2.81, 2.46, 2.13, 1.8, 1.6, 1.44, 1.28, 1.24, 1.21, 1.14, 1.08, 1.09, 1.03, 1.02],
        'URL': ['https://www.flashscorekz.com/match/2g49AZK4/#/match-summary']
    },
    {
        '1': [6.2],
        'X': [3.9],
        '2': [1.63],
        'ТОТАЛЫ': [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.5, 5, 5.5],
        'БОЛЬШЕ': [1.07, 1.08, 1.1, 1.23, 1.36, 1.46, 1.61, 1.92, 2.16, 2.48, 3.05, 3.4, 3.7, 4.7, 6.6, 7.5, 12.0, 12.0],
        'МЕНЬШЕ': [8.0, 7.5, 6.6, 3.9, 3.0, 2.66, 2.33, 1.96, 1.71, 1.52, 1.35, 1.29, 1.25, 1.18, 1.1, 1.08, 1.02, 1.02],
        'URL': ['https://www.flashscorekz.com/match/hdEvqxcM/#/match-summary']
    }
]

index = list(data[0].keys())
df: pd.DataFrame = (pd.DataFrame(data)
   .apply(pd.Series.explode)
   .pivot_table(index=index, sort=False)
   .style.applymap_index(stylize_df)
)
  • Related