如何拆分一列列表中的Pandas导出到Excel?

ujv3wf0j  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(156)

我怎样才能把下面从数值计算中得到的数据分成不同的列呢?

import random

import numpy as np
import pandas as pd

low = 0
high = 500
sample_size = 5

def get_numbers(low, high, sample_size):
    return random.sample(range(low, high), sample_size)

p_one = np.array(get_numbers(low, high, sample_size), dtype=int)
p_two = np.array(get_numbers(low, high, sample_size), dtype=int)
p_three = np.array(get_numbers(low, high, sample_size), dtype=int)
p_four = np.array(get_numbers(low, high, sample_size), dtype=int)
p_five = np.array(get_numbers(low, high, sample_size), dtype=int)

c_one = np.array(get_numbers(low, high, sample_size), dtype=int)
c_two = np.array(get_numbers(low, high, sample_size), dtype=int)
c_three = np.array(get_numbers(low, high, sample_size), dtype=int)
c_four = np.array(get_numbers(low, high, sample_size), dtype=int)
c_five = np.array(get_numbers(low, high, sample_size), dtype=int)

for idn in range(0, 10):  # ------------------n+1 for the last process step
    p = [p_one, p_two, p_three, p_four, p_five]
    c = [c_one, c_two, c_three, c_four, c_five]

df_final = pd.DataFrame(
    list(
        zip(
            p,
            c,
            "storage_workload",
            "storage_R1",
            "storage_R2",
            "storage_R3",
            "storage_R4",
            "storage_cycle_time",
            "storage_n",
            "storage_avg_opt_gap",
        )
    ),
    columns=[
        "Processing",
        "Cleaning",
        "workload",
        "R1",
        "R2",
        "R3",
        "R4",
        "Cycle time",
        "n",
        "Percentage Gap",
    ],
)

Excel中的预期结果:

谢谢你。

uxhixvfz

uxhixvfz1#

使用您提供的 Dataframe (运行您的代码时所得到的示例):

Processing                  Cleaning workload R1 R2 R3 R4  \
0    [4, 184, 330, 234, 473]  [213, 259, 360, 15, 457]        s  s  s  s  s   
1  [314, 176, 226, 304, 444]  [452, 15, 131, 141, 189]        t  t  t  t  t   
2  [313, 268, 291, 478, 285]  [14, 401, 367, 265, 187]        o  o  o  o  o   
3   [162, 31, 366, 330, 484]  [361, 140, 441, 50, 215]        r  r  r  r  r   
4   [235, 360, 473, 342, 43]   [69, 437, 496, 41, 269]        a  a  a  a  a   

  Cycle time  n Percentage Gap  
0          s  s              s  
1          t  t              t  
2          o  o              o  
3          r  r              r  
4          a  a              a

下面是使用Pandas from_recordsMultiIndex.from_product执行此操作的一种方法:
x一个一个一个一个x一个一个二个x

相关问题