我有以下資料框:
df1 = pd.DataFrame({"id": ['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4', 'C1','C2','C3','C4' ],
"date": [pd.Timestamp(2015, 12, 30), pd.Timestamp(2016, 12, 30), pd.Timestamp(2017, 12, 30), pd.Timestamp(2018, 12, 30),pd.Timestamp(2015, 12, 30), pd.Timestamp(2016, 12, 30), pd.Timestamp(2017, 12, 30), pd.Timestamp(2018, 12, 30), pd.Timestamp(2016, 12, 30), pd.Timestamp(2017, 12, 30), pd.Timestamp(2018, 12, 30), pd.Timestamp(2019, 12, 30)],
"other_col": ['NA', 'NA', 'A333', 'A444', 'NA', 'NA', 'B555', 'B666', 'NA', 'C777', 'C888', 'C999'],
"other_col_1": [123, 123, 'NA', 'NA', 0.765, 0.555, 'NA', 'NA', 0.324, 'NA', 'NA','NA']})
我想洗掉 id 列對應于“other_col”中兩次值的行,并只保留每個組的最近行。生成的資料框應為:
df_new = pd.DataFrame({"id": ['A1', 'A2', 'A4', 'B1', 'B2', 'B4', 'C1','C4' ],
"date": [pd.Timestamp(2015, 12, 30), pd.Timestamp(2016, 12, 30), pd.Timestamp(2018, 12, 30),pd.Timestamp(2015, 12, 30), pd.Timestamp(2016, 12, 30), pd.Timestamp(2018, 12, 30), pd.Timestamp(2016, 12, 30), pd.Timestamp(2019, 12, 30)],
"other_col": ['NA', 'NA', 'A444', 'NA', 'NA', 'B666', 'NA', 'C999'],
"other_col_1": [123, 123, 'NA', 0.765, 0.555, 'NA', 0.324, 'NA']})
uj5u.com熱心網友回復:
首先將值轉換NA為缺失值,other_col并在必要時對每個id和s 的值進行排序,因此可以按每個創建的沒有數字的組date獲得最后一個非缺失值,最后一個過濾器匹配具有缺失值的行:other_colGroupBy.lastidother_col
df1['other_col'] = df1['other_col'].replace('NA', np.nan)
df1 = df1.sort_values(['id','date'])
s = df1.groupby(df1['id'].str.replace('\d',''))['other_col'].transform('last')
df_new = df1[df1['other_col'].eq(s) | df1['other_col'].isna()]
print (df_new)
id date other_col other_col_1
0 A1 2015-12-30 NaN 123
1 A2 2016-12-30 NaN 123
3 A4 2018-12-30 A444 NA
4 B1 2015-12-30 NaN 0.765
5 B2 2016-12-30 NaN 0.555
7 B4 2018-12-30 B666 NA
8 C1 2016-12-30 NaN 0.324
11 C4 2019-12-30 C999 NA
uj5u.com熱心網友回復:
IIUC,您可以獲得groupby字母和 NA 狀態并獲得last:
df2 = df1.groupby([df1['id'].str[0], df1['other_col'].eq('NA')],
sort=False, as_index=False).last()
輸出:
id date other_col
0 A1 2016-12-30 NA
1 A3 2018-12-30 444
2 B1 2016-12-30 NA
3 B3 2018-12-30 222
4 C1 2016-12-30 NA
5 C4 2019-12-30 888
對于獲取 id 的更通用的方法:df1['id'].str.extract('^(\D)', expand=False)
如果您在 other_col 中有真正的 NaN,請使用 df1['other_col'].isna()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444012.html
上一篇:為什么洗掉/洗掉熊貓中的列/行會導致分配值不起作用?
下一篇:用Python去除例外值
