简体   繁体   English

用同一列中的以下值填充 Pandas 中的空行

[英]Fill empty rows in Pandas with value below from same column

I can see countless posts asking on to fill empty rows with the above value or specific values, but none about filling rows with a value of the same column but below .我可以看到无数帖子要求用上述值或特定值填充空行,但没有关于用同一列但低于.

I would like to go from this table:我想从这张桌子上走:

我的表

from a table with all lines above 3 filled with 3, and all lines above 2 and below 3 with 2, etc.从一个表中,所有高于 3 的行都用 3 填充,所有高于 2 和低于 3 的行都用 2 填充,等等。

Thanks a lot!非常感谢!

EDIT: Found it!编辑:找到了!

df[col] = df[col].bfill() - as simple as that :) df[col] = df[col].bfill() - 就这么简单:)

Try this one:试试这个:

import pandas as pd
import numpy as np

df = pd.DataFrame([[8, 2, "", 0],
                   [3, 4, "", 1],
                   [5, 3, "", 0],
                   [2, 3, 10, 4]],
                  columns=list("ABCD"))

df= df.replace(to_replace="", method='bfill').values
print(df)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM