1 回答

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以groupby與bfill和一起使用ffill。然后刪除重復(fù)項(xiàng)。
要按索引分組,請(qǐng)使用level=0:
df = pd.DataFrame([['2017/06/22', 49.8, 281.6, np.nan],
['2017/06/22', np.nan, np.nan, 36.1],
['2017/06/23', 49.6, 280.2, np.nan],
['2017/06/23', np.nan, np.nan, 35.9]],
columns=['date', 'ratio', 'local', 'usd'])
df = df.set_index('date')
g = df.groupby(level=0)
df = g.bfill().ffill().drop_duplicates()
print(df)
ratio local usd
date
2017/06/22 49.8 281.6 36.1
2017/06/23 49.6 280.2 35.9
添加回答
舉報(bào)