python pandas - Editing multiple DataFrames with a for loop

前端 未结 6 737
误落风尘
误落风尘 2021-01-06 18:30

Considering the following 2 lists of 3 dicts and 3 empty DataFrames

dict0={\'actual\': {\'2013-02-20 13:30:00\': 0.93}}
dict1={\'actual\': {\'2013-02-20 13:3         


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 19:21

    This will get it done in place!!!
    Please note the 3 exclamations

    one liner

    [dfs[i].set_value(r, c, v)
     for i, dn in enumerate(dicts)
     for r, dr in dn.items()
     for c, v in dr.items()]; 
    

    somewhat more intuitive

    for d, df in zip(dicts, dfs):
        temp = pd.DataFrame(d).stack()
        for (r, c), v in temp.iteritems():
            df.set_value(r, c, v)
    
    df0
    
                         actual
    2013-02-20 13:30:00    0.93
    

    equivalent alternative
    without the pd.DataFrame construction

    for i, dn in enumerate(dicts):
        for r, dr in dn.items():
            for c, v in dr.items():
                dfs[i].set_value(r, c, v)
    

    Why is this different?
    All the other answers, so far, reassign a new dataframe to the requisite position in the list of dataframes. They clobber the dataframe that was there. The original dataframe is left empty while a new non-empty one rests in the list.

    This solution edits the dataframe in place ensuring the original dataframe is updated with new information.

    Per OP:

    However, when trying to retrieve for instance 1 of the df outside of the loop, it is still empty


    timing
    It's also considerably faster


    setup

    dict0={'actual': {'2013-02-20 13:30:00': 0.93}}
    dict1={'actual': {'2013-02-20 13:30:00': 0.85}}
    dict2={'actual': {'2013-02-20 13:30:00': 0.98}}
    dicts=[dict0, dict1, dict2]
    
    df0=pd.DataFrame()
    df1=pd.DataFrame()
    df2=pd.DataFrame()
    dfs=[df0, df1, df2]
    

提交回复
热议问题