1 回答
TA贡献1866条经验 获得超5个赞
对于您发布的特定示例,在分组之前仅删除 NaN 即可:
df = df.dropna().groupby('id').apply(lambda x: x.iloc[:-1]).reset_index(drop=True)
df
Out[58]:
id date numeric nominal
0 1 2002-02-02 0.9 0
1 1 2003-03-03 0.4 1
2 2 2005-05-05 0.6 1
3 3 2008-08-08 0.7 0
如果您有一个不连续的 NaN 并且只想删除最后一个 NaN 块:
def strip_rows(X):
X = X.iloc[:-1, :]
while pd.isna(X.iloc[-1, 2]):
X = X.iloc[:-1, :]
return X
df_1 = pd.DataFrame({"id": [1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3],
"date": [pd.Timestamp(2002, 2, 2),
pd.Timestamp(2003, 3, 3),
pd.Timestamp(2004, 4, 4),
pd.Timestamp(2005, 5, 5),
pd.Timestamp(2006, 6, 6),
pd.Timestamp(2007, 7, 7),
pd.Timestamp(2008, 8, 8),
pd.Timestamp(2009, 9, 9),
pd.Timestamp(2010, 10, 10),
pd.Timestamp(2011, 11, 11),
pd.Timestamp(2011, 12, 12),
pd.Timestamp(2012, 1, 1)],
"numeric": [0.9, 0.4, 0.2, 0.6, np.nan, 0.8, 0.7, np.nan, np.nan, 0.5, np.nan, 0.3],
"nominal": [0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1]})
df_2 = df_1.groupby('id').apply(strip_rows).reset_index(drop=True)
df_1
Out[151]:
id date numeric nominal
0 1 2002-02-02 0.9 0
1 1 2003-03-03 0.4 1
2 1 2004-04-04 0.2 0
3 2 2005-05-05 0.6 1
4 2 2006-06-06 NaN 0
5 2 2007-07-07 0.8 0
6 3 2008-08-08 0.7 0
7 3 2009-09-09 NaN 1
8 3 2010-10-10 NaN 1
9 3 2011-11-11 0.5 1
10 3 2011-12-12 NaN 0
11 3 2012-01-01 0.3 1
df_2
Out[152]:
id date numeric nominal
0 1 2002-02-02 0.9 0
1 1 2003-03-03 0.4 1
2 2 2005-05-05 0.6 1
3 3 2008-08-08 0.7 0
4 3 2009-09-09 NaN 1
5 3 2010-10-10 NaN 1
6 3 2011-11-11 0.5 1
添加回答
举报