简体   繁体   English

根据上一个值在数据框列中填充NaN

[英]Fill NaNs in dataframe column depending on last value

I would like to fill missing (NaN) values in a column with values that depend on the last non=NaN value. 我想用依赖于最后一个non = NaN值的值填充列中的缺失(NaN)值。 My data looks this this: 我的数据看起来像这样:

In [3]: A = pd.DataFrame(['X', np.nan, np.nan, 'Y',np.nan, np.nan, 'X', np.nan])

In [4]: A
Out[4]:
     0
0    X
1  NaN
2  NaN
3    Y
4  NaN
5  NaN
6    X
7  NaN

I am aware of the fillna function, but this is not exactly what I want to do. 我知道fillna函数,但这并不是我想要的。 This gives me the following: 这给了我以下内容:

In [5]: A.fillna(method='ffill') # Not what I want to do
Out[5]:
   0
0  X
1  X
2  X
3  Y
4  Y
5  Y
6  X
7  X

For example, I would like to fill in a 'I' if the last value was 'X' and 'J' if the last value was 'Y'. 例如,如果最后一个值是“ X”,我想填写一个“ I”,如果最后一个值是“ Y”,我想填写一个“ J”。 Ie

Out[5]: # How do I get this?
   0
0  X
1  I
2  I
3  Y
4  J
5  J
6  X
7  I

I am sure I could do this with a loop, but how do I do it without resorting to that? 我确定我可以循环执行此操作,但是如何不使用该方法呢?

You can create a dictionary mapping the preceding value to the desired fill value, then use fillna with an forward filled version of the DataFrame with the mapping applied by using replace and ffill : 您可以创建一个将先前值映射到所需填充值的字典,然后将fillnafillna的前向填充版本一起使用,并使用replaceffill来应用映射:

nan_map = {'X': 'I', 'Y': 'J'}
A = A.fillna(A.replace(nan_map).ffill())

The resulting output: 结果输出:

   0
0  X
1  I
2  I
3  Y
4  J
5  J
6  X
7  I

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在数据框的列中填充连续NAN - Fill Consecutive NANs in a column of a dataframe 对于数据框中的每个列和单元格,使用该列中的随机值填写NaN / Null - For every column and cell in dataframe fill in NaNs/Nulls with random value from that column 使用来自另一个 dataframe 的列的值填充列,具体取决于条件 - fill column with value of a column from another dataframe, depending on conditions 快速获取从DataFrame中的最后一个有效值开始计算的一列中NaN数的快速方法 - Fast way to get the number of NaNs in a column counted from the last valid value in a DataFrame 如果列包含某个值,则替换 dataframe 中的 Nans - Replace Nans in dataframe if a column contains a certain value 用dask dataframe中的每列最大值填充NaN - Fill NaNs with per-column max in dask dataframe 根据条件在另一列上填充 pandas.DataFrame 的 NaN - Fill NaNs of pandas.DataFrame based on condition over another column 如何用最频繁的项目填充 dataframe 列中的 NaN? - How to fill NaNs in a dataframe column with its most frequent item? 根据熊猫数据框中另一列的最后一个值填充列 - Fill columns based on the last value of another column in a pandas dataframe 如何根据匹配值的辅助数据框的条件在主数据框的列中填充 NaN 以使用多个填充值填充 NaN - How to Fill NaNs in Column of Main Dataframe Based On Conditions Matching Secondary Dataframe of Values to Fill NaNs With Multiple Filler Values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM