找到了一个使用.fillna的解决方案
你可以猜到,我的头衔已经让人困惑了,我也是! 我有一个这样的数据帧
Index Values
0 NaN
1 NaN
...................
230 350.21
231 350.71
...................
1605 922.24
在230和1605之间,我有值,但不适用于前229个条目。所以我计算了斜率来近似丢失的数据,并将其存储在“slope”中。你知道吗
Y1 = df['Values'].min()
X1ID = df['Values'].idxmin()
Y2 = df['Values'].max()
X2ID = df['Values'].idxmax()
slope = (Y2 - Y1)/(X2ID - X1ID)
实际上,我想从值中得到.min,减去斜率,然后在前一个.min之前的索引中插入新值。但是,我完全迷路了,我尝试了以下方法:
df['Values2'] = df['Values'].min().apply(lambda x: x.min() - slope)
但这显然是垃圾。我非常感谢你的建议
编辑
因此,在尝试了多种方法之后,我找到了一个至少对我有效的粗略解决方案。你知道吗
loopcounter = 0
missingValue = []
missingindex = []
missingindex.append(loopcounter)
missingValue.append(Y1)
for minValue in missingValue:
minValue = minValue-slopeave
missingValue.append(minwavelength)
loopcounter +=1
missingindex.append(loopcounter)
if loopcounter == 230:
break
del missingValue[0]
missingValue.reverse()
del missingindex[-1]
首先,我创建了两个列表,一个用于缺少的值,另一个用于索引。 之后,我将我的最小值(Y1)添加到列表中并开始循环。 我希望循环在230次之后停止(丢失值的数量) 每个循环将从列表中的项中减去斜率,从最小值开始,同时将计数器添加到missingindex列表中。你知道吗
删除第一个值并反转顺序将列表转换为正确的顺序。你知道吗
missValue = dict(zip(missingindex,missingValue))
然后我把这两个单子合并成一本字典
df['Values'] = df['Values'].fillna(missValue)
之后,我使用.fillna函数用字典填充我的数据帧。你知道吗
这对我很有效,我知道这可能不是最优雅的解决方案。。。你知道吗
我要感谢所有花时间帮助我的人,非常感谢。你知道吗
看看这个。不过,我觉得你要把这是一个循环,因为插入和最小值的计算必须做重新计算
这里提供了插入逻辑Is it possible to insert a row at an arbitrary position in a dataframe using pandas?
我想你可以用^{} 和^{} :
我会稍微修改一下:
编辑
或者试着把这个放在下面这样的循环中。这将递归地填充所有值,直到到达序列的末尾:
这样称呼:
我希望这有帮助!你知道吗
相关问题 更多 >
编程相关推荐