简体   繁体   中英

How iterate over rows in a dataframe dictionnary and change some values - Pandas Python

I have this dictionary of df:

DataFrameDict:
'A':                         
                       date_time    begin                            end
      2019-10-21 07:39:07.927729      NaT    2019-10-21  07:42:24.747350
      2019-10-21 07:39:26.356716      NaT    2019-10-21  07:42:02.574268
      2019-10-21 07:40:03.235327      NaT    2019-10-21  07:42:02.222821


'B':                         
                      date_time                             begin                           end
     2019-10-21 07:39:07.927729       2019-10-21  07:42:24.747350                           NaT    
     2019-10-21 07:39:26.356716       NaT                           2019-10-21  07:42:02.574268
     2019-10-21 07:40:03.235327       NaT                           2019-10-21  07:42:02.222821
                      

I would like to do this : for each row of each dataframe, if begin[i] == NaT then begin[i] = date_time[i] else: end[i] = date_time[i].

The result must be :

DataFrameDict:
'A':                         
                       date_time                           begin                            end
      2019-10-21 07:39:07.927729      2019-10-21 07:39:07.927729    2019-10-21  07:42:24.747350
      2019-10-21 07:39:26.356716      2019-10-21 07:39:26.356716    2019-10-21  07:42:02.574268
      2019-10-21 07:40:03.235327      2019-10-21 07:40:03.235327    2019-10-21  07:42:02.222821


'B':                         
                      date_time                             begin                           end
     2019-10-21 07:39:07.927729       2019-10-21  07:42:24.747350   2019-10-21 07:39:07.927729    
     2019-10-21 07:39:26.356716       2019-10-21 07:39:26.356716    2019-10-21  07:42:02.574268
     2019-10-21 07:40:03.235327       2019-10-21 07:40:03.235327    2019-10-21  07:42:02.222821
                      

So I try this piece of code of mine :

 for key in DataFrameDict.keys():
    for row in DataFrameDict[key].itertuples():
        if DataFrameDict[key].at[row.Index, 'begin'] == 'NaT':
            DataFrameDict[key].at[row.Index, 'begin'] = DataFrameDict[key].at[row.Index, 'date_time']
        else:
            DataFrameDict[key].at[row.Index, 'end'] = DataFrameDict[key].at[row.Index, 'date_time']

But it doesn't work...

Thanks for your time !

UPDATE I tried your soluton Quang Hoang

for key in DataFrameDict.keys():
    mask = DataFrameDict[key]['begin'].isna()
    DataFrameDict[key].loc[mask, 'begin'] = DataFrameDict[key]['date_time']
    DataFrameDict[key].loc[~mask, 'end'] = DataFrameDict[key]['date_time']

the data's results aren't correct.

Let's try this function:

def fill_date(df):
    mask = df['begin'].isna()
    df.loc[mask,'begin'] = df['date_time']
    df.loc[~mask, 'end'] = df['date_time']

for df in [A,B]: fill_date(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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