![](/img/trans.png)
[英]Python: How to use a list comprehension to replace Nones in one list with values from another list?
[英]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.