pandas 用第一个可用值的一部分填充nan值 [英] Pandas fill nan values with a split of the first available value
问题描述
我正在尝试使用以下所有nan值中的第一个先前的可用值替换DataFrame中的nan值。
I'm trying to replace nan values in a DataFrame with a split of the first previous available value across all the following nan values.
在下面的示例中:
import pandas as pd
df = [100, None, None, 40, None, 120]
df = pd.DataFrame(df)
我想获取:
[33.33, 33.33, 33.33, 20, 20, 120]
如果我可以找到一种方法来对列中每个值之后的nan值进行计数,那么我可以运行一些计算来实现拆分。
If I could find a way to count the number of nan values following each value in my column, then I could run some computations to achieve the split.
推荐答案
使用:
import pandas as pd
df = [100, None, None, 40, None, 120]
df = pd.DataFrame(df, columns=['a'])
s = df['a'].ffill() / df.groupby(df['a'].notna().cumsum())['a'].transform('size')
print (s)
0 33.333333
1 33.333333
2 33.333333
3 20.000000
4 20.000000
5 120.000000
Name: a, dtype: float64
详细信息:
您可以用之前的 NaN
值代替以前的缺失值填充
:
You can replace missing value by previous non NaN
s values by ffill
:
print (df['a'].ffill())
0 100.0
1 100.0
2 100.0
3 40.0
4 40.0
5 120.0
Name: a, dtype: float64
然后通过 Series.notna
并通过 Series.cumsum
:
Then compare by Series.notna
and create groups by Series.cumsum
:
print (df['a'].notna().cumsum())
0 1
1 1
2 1
3 2
4 2
5 3
Name: a, dtype: int32
并获得与原始w相同大小的每个组的计数ith GroupBy.transform
:
And get counts per groups with same size like original with GroupBy.transform
:
print (df.groupby(df['a'].notna().cumsum())['a'].transform('size'))
0 3
1 3
2 3
3 2
4 2
5 1
Name: a, dtype: int64
这篇关于 pandas 用第一个可用值的一部分填充nan值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!