分组数据帧中的Pandas | Fillna(ffill)没有fi

2024-05-16 01:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个MultIndex数据帧,并试图填充一个值MAX_PTS_YR,这样t+1中的MAX_PTS_YR等于t中的MAX_PTS_YR。在

所以:2016中的MAX_PTS_YR应该等于116。在

使用nth,我找到了上一年的MAX_PTS

DF['MAX_PTS_YR'] = DF.groupby(by=['Affiliation','Year'],as_index=False)['PtsYr'].nth(-1)


Affiliation mkid        Year    PtsYr  MAX_PTS_YR
MVPAFL0003  10176228    2015    96.0    NaN
MVPAFL0003  10176228    2015    96.0    NaN
MVPAFL0003  10176228    2015    106.0   NaN
MVPAFL0003  10176228    2015    116.0   116.0
MVPAFL0003  10176228    2016    10.0    NaN
MVPAFL0003  10176228    2016    10.0    NaN
MVPAFL0003  10176228    2016    20.0    NaN
MVPAFL0003  10176228    2016    20.0    NaN
MVPAFL0003  10176228    2016    30.0    NaN
MVPAFL0003  10176228    2016    40.0    NaN
MVPAFL0003  10176228    2016    50.0    NaN
MVPAFL0003  10176228    2016    50.0    NaN
MVPAFL0003  10176228    2016    52.0    NaN
MVPAFL0003  10176228    2016    62.0    NaN
MVPAFL0003  10176228    2016    62.0    NaN
MVPAFL0003  10176228    2016    82.0    NaN
MVPAFL0003  10176228    2016    94.0    NaN
MVPAFL0003  10176228    2016    94.0    NaN
MVPAFL0003  10176228    2016    94.0    NaN
MVPAFL0003  10176228    2016    104.0   NaN
MVPAFL0003  10176228    2016    114.0   114.0

我想我可以fillnaAffiliation小组:

^{pr2}$

但是当我这样做时,没有填充NaN值。在

有什么想法?在


Tags: 数据dfbyasnanyearmaxpts
2条回答
# get just the series you are filling to simplify things
s1 = df.set_index(['Affiliation', 'Year']).MAX_PTS_YR

# groupby to get the max per group
mx = s1.groupby(level=[0, 1]).max()

# shift your year index by one year
mx.index.set_levels(mx.index.levels[1] + 1, 1, inplace=True)

# fill in missing bits
s1.fillna(mx)

^{pr2}$

现在分配给df

df.MAX_PTS_YR = (s1.fillna(mx).values)
df

enter image description here

如果这是唯一一个数据为空的列,可以对整个数据帧进行操作:

DF.ffill(inplace=True)

请注意,即使最初以整数形式输入点数,也会返回浮点值。从技术上讲,这是整个柱体的力。要获得整数(这可能是您想要的,除非您可以有部分点),请执行以下操作:

^{pr2}$

也许你也应该对PTS专栏做些什么。在

相关问题 更多 >