簡體   English   中英

將列表中的“無”替換為從鄰居傳播的值

[英]Replace Nones in List with Values propagated from neighbors

我有一個清單:

L = [-10,-9.8,-9.6, None, None, None, -8.8, -8.6, -8.4]

如何根據值的傳播將None替換為值,因此看起來像這樣:

L = [-10,-9.8,-9.6, -9.4, -9.2, -9.0, -8.8, -8.6, -8.4]

我嘗試使用pandas.fillna(method = ffill)但它給ValueError: DataFrame constructor not properly called! 但這似乎並不是正確的方法。

您可以這樣做:

import pandas as pd

pd.Series([-10,-9.8,-9.6, None, None, None, -8.8, -8.6, -8.4]).interpolate()

收益率:

0   -10.0
1    -9.8
2    -9.6
3    -9.4
4    -9.2
5    -9.0
6    -8.8
7    -8.6
8    -8.4
dtype: float64

返回列表:

pd.Series([-10,-9.8,-9.6, None, None, None, -8.8, -8.6, -8.4]).interpolate().tolist()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM