简体   繁体   English

带有数据框的嵌套字典到熊猫中的数据框

[英]Nested dictionary with dataframes to dataframe in pandas

I know that there are a few questions about nested dictionaries to dataframe but their solutions do not work for me. 我知道有一些关于嵌套字典到数据框的问题,但是它们的解决方案对我不起作用。 I have a dataframe, which is contained in a dictionary, which is contained in another dictionary, like this: 我有一个数据框,该数据框包含在一个字典中,该数据框包含在另一个字典中,如下所示:

df1 = pd.DataFrame({'2019-01-01':[38],'2019-01-02':[43]},index = [1,2])
df2 = pd.DataFrame({'2019-01-01':[108],'2019-01-02':[313]},index = [1,2])
da = {}
da['ES']={}
da['ES']['TV']=df1
da['ES']['WEB']=df2

What I want to obtain is the following: 我想获得以下内容:

df_final = pd.DataFrame({'market':['ES','ES','ES','ES'],'device':['TV','TV','WEB','WEB'],
                     'ds':['2019-01-01','2019-01-02','2019-01-01','2019-01-02'],
                     'yhat':[43,38,423,138]})

Getting the code from another SO question I have tried this: 从另一个SO问题获取代码,我已经尝试过:

market_ids = []
frames = []
for market_id,d in da.items():
  market_ids.append(market_id)
  frames.append(pd.DataFrame.from_dict(da,orient = 'index'))    
df = pd.concat(frames, keys=market_ids)

Which gives me a dataframe with multiple indexes and the devices as column names. 这给了我一个具有多个索引的数据帧,并且设备作为列名。

Thank you 谢谢

The code below works well and gives the desired output: 下面的代码运行良好,并提供了所需的输出:

t1=da['ES']['TV'].melt(var_name='ds', value_name='yhat')
t1['market']='ES'
t1['device']='TV'

t2=da['ES']['WEB'].melt(var_name='ds', value_name='yhat')
t2['market']='ES'
t2['device']='WEB'

m = pd.concat([t1,t2]).reset_index().drop(columns={'index'})

print(m)

And the output is: 输出为:

           ds  yhat market device
0  2019-01-01    38     ES     TV
1  2019-01-02    43     ES     TV
2  2019-01-01   108     ES    WEB
3  2019-01-02   313     ES    WEB

The main takeaway here is melt function, which if you read about isn't that difficult to understand what's it doing here. 这里主要介绍的是melt函数,如果您读过它,那么不难理解它在做什么。 Now as I mentioned in the comment above, this can be done iteratively over whole da named dictionary, but to perform that I'd need replicated form of the actual data. 现在,正如我在上面的评论中提到的那样,可以在整个命名词典中迭代完成此操作,但是要执行此操作,我需要复制实际数据的形式。 What I intended to do was to take this first t1 as the initial dataframe and then keep on concatinating others to it, which should be really easy. 我打算做的是将第一个t1用作初始数据帧,然后继续对其隐瞒其他内容,这确实很容易。 But I don't know how your actual values are. 但我不知道您的实际价值如何。 But I am sure you can figure out on your own from above how to put this under a loop. 但是我敢肯定,您可以从上面自己弄清楚如何将其置于一个循环中。

The pseudo code for that loop thing I am talking about would be like this: 我正在谈论的那个循环事情的伪代码将是这样的:

real=t1
for a in da['ES'].keys():
    if a!='TV':

        p=da['ES'][a].melt(var_name='ds', value_name='yhat')
        p['market']='ES'
        p['device']=a

        real = pd.concat([real,p],axis=0,sort=True)

real.reset_index().drop(columns={'index'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM