这是我的数据框,我有一个关于时间和 ID 的多索引。
+------+-------+----------+-----------+
| col1 | col2 | col3 | col4 |
+-------+------+------+-------+----------+-----------+
| ID | t | | | | |
+-------+------+------+-------+----------+-----------+
| id1 | t1 | 10 | nan | nan | 1 |
| id1 | t2 | 10 | 110 | 1 | nan |
| id1 | t3 | 12 | nan | nan | nan |
| id2 | t1 | 12 | 109 | 15 | 1 |
| id2 | t4 | 12 | 109 | nan | 1 |
| id2 | t7 | 20 | nan | nan | nan |
+-------+------+------+-------+----------+-----------+
是否可以仅在 col3 和 col4 上进行多索引 fwd 填充?
+------+-------+----------+-----------+
| col1 | col2 | col3 | col4 |
+-------+------+------+-------+----------+-----------+
| ID | t | | | | |
+-------+------+------+-------+----------+-----------+
| id1 | t1 | 10 | nan | nan | 1 |
| id1 | t2 | 10 | 110 | 1 | 1 |
| id1 | t3 | 12 | nan | 1 | 1 |
| id2 | t1 | 12 | 109 | 15 | 1 |
| id2 | t4 | 12 | 109 | 15 | 1 |
| id2 | t7 | 20 | nan | 15 | 1 |
+-------+------+------+-------+----------+-----------+
到目前为止我已经尝试过:
df[['col3','col4']].ffill() #how to account for the multiindex?
df[['col3','col4']].fillna(df.groupby(['ID','t'])[['col3', 'col4']].ffill()) #did not work
df.reindex(['ID','t'], method='ffill') #this is probably incomplete, and I got 'expected Tuple, got str'