将df转换为熊猫的特定字典词典

时间:2020-08-19 14:26:33

标签: python-3.x pandas dataframe dictionary

我有一个如下所示的df

Params                       Value
teachers                        49
students                       289
R                              3.7
holidays                       165
OS                              18
Em_from   2020-02-29T20:00:00.000Z
Em_to     2020-03-20T20:00:00.000Z
Em_F                             3
Em_C                             2
sC_from   2020-03-31T20:00:00.000Z
sC_to     2020-05-29T20:00:00.000Z
sC_F                            25
sC_C                            31

从上面的df中,我想将其转换为字典的字典,如下所示。

字典:

{'teachers': 49, 
 'students': 289,  
 'R': 3.7, 
 'holidays': 165,
 'OS':18,
 'Em': {'from': '2020-02-29T20:00:00.000Z', 'to': '2020-03-20T20:00:00.000Z', 
       'F': 3, 'C': 2},
 'sC': {'from': '2020-03-31T20:00:00.000Z', 'to': '2020-05-29T20:00:00.000Z', 
        'F': 25, 'C': 31}}

3 个答案:

答案 0 :(得分:1)

使用:

validate :future_date, on: :a
validate :between_date, on: :b

def between_date
 past_date
 future_date
end

结果:

s = df['Params'].str.split('_')
m = s.str.len().eq(1)

d1 = df[m].set_index('Params')['Value'].to_dict()
d2 = df[~m].assign(Params=s.str[-1]).agg(tuple, axis=1)\
           .groupby(s.str[0]).agg(lambda s: dict(s.tolist())).to_dict()

dct = {**d1, **d2}

答案 1 :(得分:1)

请始终尝试以可重复的方式提供数据,更多的人将能够尝试该问题

数据集

Params = ['teachers','students','R','holidays','OS','Em_from','Em_to','Em_F','Em_C','sC_from','sC_to','sC_F','sC_C']                       

Value = ['49','289','3.7','165','18','2020-02-29T20:00:00.000Z','2020-03-20T20:00:00.000Z','3','2','2020-03-31T20:00:00.000Z','2020-05-29T20:00:00.000Z','25','31']

df = pd.DataFrame(zip(Params,Value),columns=["col1","col2"])

您可以做类似的事情

d = {}
for lst in df.values:
    for k,v in zip(lst[0:],lst[1:]):
        if any(name in k for name in ('Em_from', 'sC_from')):d[k.split('_')[0]] = {k.split('_')[1]:v}
        elif any(name in k for name in ('Em_to', 'Em_F','Em_C','sC_to','sC_F','sC_C')):d[k.split('_')[0]][k.split('_')[1]] = v
        else:d[k] = v

输出

{'teachers': '49',
 'students': '289',
 'R': '3.7',
 'holidays': '165',
 'OS': '18',
 'Em': {'from': '2020-02-29T20:00:00.000Z',
  'to': '2020-03-20T20:00:00.000Z',
  'F': '3',
  'C': '2'},
 'sC': {'from': '2020-03-31T20:00:00.000Z',
  'to': '2020-05-29T20:00:00.000Z',
  'F': '25',
  'C': '31'}}

答案 2 :(得分:0)

熊猫的数据帧具有to_json方法(see docs)

那里有多个示例,但是总体流程是这样的,假设您有一个名为df的数据框:

import json
import pandas as pd

parsed = df.to_json()
df_json = json.loads(json_df)

阅读文档以查看更多示例和您可能需要弄弄的其他参数。