从字典而不是Pandas中写入文件

2024-04-19 23:19:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用另一种方式打印词典存档。 现在,我正在使用Pandas将字典转换为Dataframes,组合几个Dataframes,然后将它们打印到文件中(参见下面的代码)。 不过,熊猫的行动似乎需要很长时间,我希望这样做更有效率。你知道吗

在保留输出文件结构的同时,是否可以更有效地执行以下方法?(例如,直接从字典打印?)你知道吗

import pandas as pd

labels = ["A", "B", "C"]
periods = [0, 1, 2]
header = ['key', 'scenario', 'metric', 'labels']
metrics_names = ["metric_balances", "metric_record"]

key = "key_x"
scenario = "base"
# The metrics are structured as dicts where the keys are `periods` and the values 
# are arrays (where each array entry correspond to one of the `labels`)
metric_balances = {0: [1000, 100, 50], 1: [900, 150, 100], 2: [800, 350, 100]}
metric_record = {0: [20, 10, 5], 1: [90, 15, 10], 2: [80, 35, 10]}

# Combine all metrics into one output structure for key "x"
output_x = pd.concat([pd.DataFrame(metric_balances, columns=periods, index=labels),
                      pd.DataFrame(metric_record, columns=periods, index=labels)],
                     keys=pd.MultiIndex.from_product([[key], [scenario], metrics_names]), 
                     names=header)

key = "key_y"
scenario = "base_2"
metric_balances = {0: [2000, 200, 50], 1: [1900, 350, 100], 2: [1200, 750, 100]}
metric_record = {0: [40, 5, 3], 1: [130, 45, 10], 2: [82, 25, 18]}

# Combine all metrics into one output structure for key "y"
output_y = pd.concat([pd.DataFrame(metric_balances, columns=periods, index=labels),
                      pd.DataFrame(metric_record, columns=periods, index=labels)],
                     keys=pd.MultiIndex.from_product([[key], [scenario], metrics_names]), 
                     names=header)

# Concatenate all output dataframes
output = pd.concat([output_x, output_y], names=header)
# Print results to a csv file
output.to_csv("test.csv", index=False)

以下是各自的输出:

OUTPUT X
                                           0    1    2
key    scenario metric          labels                
key_x  base     metric_balances A       1000  900  800
                                B        100  150  350
                                C         50  100  100
                metric_record   A         20   90   80
                                B         10   15   35
                                C          5   10   10

-----------------------------------
OUTPUT Y
                                           0     1     2
key    scenario metric          labels                  
key_y  base_2   metric_balances A       2000  1900  1200
                                B        200   350   750
                                C         50   100   100
                metric_record   A         40   130    82
                                B          5    45    25
                                C          3    10    18

------------------------------
OUTPUT COMBINED
                                           0     1     2
key    scenario metric          labels                  
key_x  base     metric_balances A       1000   900   800
                                B        100   150   350
                                C         50   100   100
                metric_record   A         20    90    80
                                B         10    15    35
                                C          5    10    10
key_y  base_2   metric_balances A       2000  1900  1200
                                B        200   350   750
                                C         50   100   100
                metric_record   A         40   130    82
                                B          5    45    25
                                C          3    10    18

我正在研究字典的行打印-但是我很难将labels与相关数组合并。你知道吗


Tags: keydataframeoutputbaseindexlabelsnamesrecord