分组方式,前移和后移填充

问题描述 投票:0回答:1

我有这个df:

ID         Date   Time       Lat       Lon
 A  07/16/2019   08:00  29.39291 -98.50925
 A  07/16/2019   09:00  29.39923 -98.51256
 A  07/16/2019   10:00  29.40147 -98.51123
 A  07/18/2019   08:30  29.38752 -98.52372
 A  07/18/2019   09:30  29.39291 -98.50925
 B  07/16/2019   08:00  29.39537 -98.50402
 B  07/18/2019   11:00  29.39343 -98.49707
 B  07/18/2019   12:00  29.39291 -98.50925
 B  07/19/2019   10:00  29.39556 -98.53148

我想通过IDDate对df进行分组,将行向后移一级,并用正向填充来填充NaN值。

注意:(ID, Date)仅一行,应由此行填充。

例如:B 07/16/2019 08:00 29.39537 -98.50402

预期结果:

ID         Date   Time       Lat       Lon Time.1     Lat.1     Lon.1
 A  07/16/2019   08:00  29.39291 -98.50925  09:00  29.39923 -98.51256
 A  07/16/2019   09:00  29.39923 -98.51256  10:00  29.40147 -98.51123
 A  07/16/2019   10:00  29.40147 -98.51123  10:00  29.40147 -98.51123
 A  07/18/2019   08:30  29.38752 -98.52372  09:30  29.39291 -98.50925
 A  07/18/2019   09:30  29.39291 -98.50925  09:30  29.39291 -98.50925
 B  07/16/2019   08:00  29.39537 -98.50402  08:00  29.39537 -98.50402
 B  07/18/2019   11:00  29.39343 -98.49707  12:00  29.39291 -98.50925
 B  07/18/2019   12:00  29.39291 -98.50925  12:00  29.39291 -98.50925
 B  07/19/2019   10:00  29.39556 -98.53148  10:00  29.39556 -98.53148

我正在使用的代码(不符合预期的结果:

pd.concat([df, df.groupby(['ID','Date']).shift(-1).ffill()], axis=1)
python pandas dataframe
1个回答
1
投票

这是一种方法:

def grp_col(f):
    f['Time.1'] = f['Time'].shift(-1).ffill().fillna(f['Time'].iloc[0])
    f['Lat.1'] = f['Lat'].shift(-1).ffill().fillna(f['Lat'].iloc[0])
    f['Lon.1'] = f['Lon'].shift(-1).ffill().fillna(f['Lon'].iloc[0])
    return f

df = df.groupby(['ID','Date'], as_index=False).apply(grp_col)
© www.soinside.com 2019 - 2024. All rights reserved.