【问题标题】:Python: pandas groupby to dictionary based on indexPython:pandas groupby 到基于索引的字典
【发布时间】:2018-12-01 13:04:21
【问题描述】:
df_devices = df.groupby(['from_time', 'device', 'type'])['power'].mean()

计算某列上groupbymean() 后,我有一个包含以下内容的pandas 系列

from_time  device         type   
00:00:00   AC             indoor     1362.214286
                          outdoor    3470.705882
           Computer       indoor      399.000000
                          outdoor     412.400000
           Heater         indoor     2258.375000
                          outdoor    2274.666667
           Lights         indoor     1535.000000
                          outdoor    3475.473684
           Microwave      indoor     1420.000000
                          outdoor    1489.933333
           Refridgerator  indoor      192.388889
                          outdoor     195.076923
           Television     indoor      243.666667
                          outdoor     261.500000
01:00:00   AC             indoor     1493.071429
                          outdoor    3724.352941
           Computer       indoor      416.461538
                          outdoor     413.555556
           Heater         indoor     2143.277778
                          outdoor    2286.461538
           Lights         indoor     1447.950000
                          outdoor    3092.454545
           Microwave      indoor     1536.857143
                          outdoor    1429.294118
           Refridgerator  indoor      207.416667
                          outdoor     234.684211
           Television     indoor      251.000000
                          outdoor     249.904762

我想把它转换成如下格式的字典:

{'00:00:00' : {'AC': {'indoor': 1362.2142857142858, 'outdoor': 3470.705882352941}, 
'Computer': {'indoor': 399.0, 'outdoor': 412.4}, 'Heater': {'indoor': 
2258.375, 'outdoor': 2274.6666666666665}, 'Lights': {'indoor': 1535.0, 
'outdoor': 3475.4736842105262}, 'Microwave': {'indoor': 1420.0, 'outdoor': 
1489.9333333333334}, 'Refridgerator': {'indoor': 192.38888888888889, 
'outdoor': 195.07692307692307}, 'Television': {'indoor': 243.66666666666666, 
'outdoor': 261.5}},
'00:00:01' : {'AC': {'indoor': 1493.0714285714287, 'outdoor': 3724.3529411764707}, 
'Computer': {'indoor': 416.46153846153845, 'outdoor': 413.55555555555554}, 
'Heater': {'indoor': 2143.277777777778, 'outdoor': 2286.4615384615386}, 
'Lights': {'indoor': 1447.95, 'outdoor': 3092.4545454545455}, 'Microwave': 
{'indoor': 1536.857142857143, 'outdoor': 1429.2941176470588}, 
'Refridgerator': {'indoor': 207.41666666666666, 'outdoor': 
234.68421052631578}, 'Television': {'indoor': 251.0, 'outdoor': 
249.9047619047619}}}

我确实尝试了框架上的.agg().unstack(),但最终得到了不同的格式 .to_dict() 也没有给出有希望的结果。它返回不同的格式

使用以下代码我能做到的最好的事情

df_devices.unstack(level=2).unstack().to_dict('index')

没有给出预期的输出

{'00:00:00': {('indoor', 'AC'): 1362.2142857142858,
('indoor', 'Computer'): 399.0,
('indoor', 'Heater'): 2258.375,
('indoor', 'Lights'): 1535.0,
('indoor', 'Microwave'): 1420.0,
('indoor', 'Refridgerator'): 192.38888888888889,
('indoor', 'Television'): 243.66666666666666,
('outdoor', 'AC'): 3470.705882352941,
('outdoor', 'Computer'): 412.4,
('outdoor', 'Heater'): 2274.6666666666665,
('outdoor', 'Lights'): 3475.4736842105262,
('outdoor', 'Microwave'): 1489.9333333333334,
('outdoor', 'Refridgerator'): 195.07692307692307,
('outdoor', 'Television'): 261.5}

【问题讨论】:

标签: python pandas series


【解决方案1】:

我假设数据框是逗号分隔的数据框,并以名称 a.csv 存储。 以下是一种方法,您可以遍历每一行并从数据框中形成字典。

df = pd.read_csv('a.csv')
df.rename(columns={'Unnamed: 3': 'value'}, inplace=True)

from_time = None
device = None
type=None
output = {}
for idx, row in df.iterrows():
    if not pd.isnull(row['from_time']):
        from_time = row['from_time']
    if not pd.isnull(row['device']):
        device = row['device']
    if from_time not in output:
        output[from_time] = {}
    if device not in output[from_time]:
        output[from_time][device] = {}
    output[from_time][device][row['type']] = row['value']

如果您发现比这更好的东西,请告诉我。 谢谢。希望对您有所帮助。

【讨论】:

  • 我用不同的方法解决了这个问题。见这里my answer
【解决方案2】:

我能够使用我创建的 dict 解决问题

dicto = df_devices.unstack(level=2).unstack().to_dict('index')

>>> dicto

{'00:00:00': {('indoor', 'AC'): 1362.2142857142858,
('indoor', 'Computer'): 399.0,
('indoor', 'Heater'): 2258.375,
('indoor', 'Lights'): 1535.0,
('indoor', 'Microwave'): 1420.0,
('indoor', 'Refridgerator'): 192.38888888888889,
('indoor', 'Television'): 243.66666666666666,
('outdoor', 'AC'): 3470.705882352941,
('outdoor', 'Computer'): 412.4,
('outdoor', 'Heater'): 2274.6666666666665,
('outdoor', 'Lights'): 3475.4736842105262,
('outdoor', 'Microwave'): 1489.9333333333334,
('outdoor', 'Refridgerator'): 195.07692307692307,
('outdoor', 'Television'): 261.5}

遍历创建的字典,

df_dict = {}
df_type = {}

for i, j in dicto.items():
    for key, value in j.items():
        df_type[key[1]] = value
        df_dict[key[0]] = df_type.copy()
    dicto[i] = df_dict.copy()

.copy() 是创建副本而不是引用

>>> dicto
{'00:00:00': {'AC': {'indoor': 1362.2142857142858,
 'outdoor': 3470.705882352941},
 'Computer': {'indoor': 399.0, 'outdoor': 412.4},
 'Heater': {'indoor': 2258.375, 'outdoor': 2274.6666666666665},
 'Lights': {'indoor': 1535.0, 'outdoor': 3475.4736842105262},
 'Microwave': {'indoor': 1420.0, 'outdoor': 1489.9333333333334},
 'Refridgerator': {'indoor': 192.38888888888889,
 'outdoor': 195.07692307692307},
 'Television': {'indoor': 243.66666666666666, 'outdoor': 261.5}}.........

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 2021-02-11
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2017-08-09
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多