Python 3.x 熊猫在不同的索引上向前和向后填充
我有以下数据帧df:Python 3.x 熊猫在不同的索引上向前和向后填充,python-3.x,pandas,fill,Python 3.x,Pandas,Fill,我有以下数据帧df: length timestamp width name testschip-1 NaN 2019-08-01 00:00:00 NaN testschip-1 NaN 2019-08-01 00:00:09 NaN testschip-1 2 2019-08-01 00:00:20 NaN tes
length timestamp width
name
testschip-1 NaN 2019-08-01 00:00:00 NaN
testschip-1 NaN 2019-08-01 00:00:09 NaN
testschip-1 2 2019-08-01 00:00:20 NaN
testschip-1 2 2019-08-01 00:00:27 NaN
testschip-1 NaN 2019-08-01 00:00:38 1
testschip-2 4 2019-08-01 00:00:39 2
testschip-2 4 2019-08-01 00:00:57 NaN
testschip-2 4 2019-08-01 00:00:58 NaN
testschip-2 NaN 2019-08-01 00:01:17 NaN
testschip-3 NaN 2019-08-01 00:02:27 NaN
testschip-3 NaN 2019-08-01 00:03:47 NaN
首先,我想从索引“name”中删除字符串“testschip-”,以便只在索引上获取整数。第二,对于每个唯一索引,我希望在“length”和“width”列上应用正向填充或反向填充(任何不需要NAN的操作)。每个唯一索引具有相同的“长度”和“宽度”。在“testschip-3”中,我不想应用向后或向前填充。如果我对“testschip-1”(设置前两个索引two'2')进行反向填充,那么最后一行索引“testschip-1”将得到一个不需要的“4”)。我无法预先判断我是否必须预先应用向后填充或向前填充,因为我有400万行数据要开始 使用:
df.index = df.index.str.lstrip('testschip-').astype(int)
#alternative
#df.index = df.index.str[10:].astype(int)
#df.index = df.index.str.split('-').str[-1].astype(int)
df.groupby(level = 0).apply(lambda x: x.bfill().ffill())
输出
length timestamp width
name
1 2.0 2019-08-01 00:00:00 1.0
1 2.0 2019-08-01 00:00:09 1.0
1 2.0 2019-08-01 00:00:20 1.0
1 2.0 2019-08-01 00:00:27 1.0
1 2.0 2019-08-01 00:00:38 1.0
2 4.0 2019-08-01 00:00:39 2.0
2 4.0 2019-08-01 00:00:57 2.0
2 4.0 2019-08-01 00:00:58 2.0
2 4.0 2019-08-01 00:01:17 2.0
3 NaN 2019-08-01 00:02:27 NaN
3 NaN 2019-08-01 00:03:47 NaN
使用:
输出
length timestamp width
name
1 2.0 2019-08-01 00:00:00 1.0
1 2.0 2019-08-01 00:00:09 1.0
1 2.0 2019-08-01 00:00:20 1.0
1 2.0 2019-08-01 00:00:27 1.0
1 2.0 2019-08-01 00:00:38 1.0
2 4.0 2019-08-01 00:00:39 2.0
2 4.0 2019-08-01 00:00:57 2.0
2 4.0 2019-08-01 00:00:58 2.0
2 4.0 2019-08-01 00:01:17 2.0
3 NaN 2019-08-01 00:02:27 NaN
3 NaN 2019-08-01 00:03:47 NaN