gpt4 book ai didi

python - 在 Pandas 数据框中查找连续的 Nans

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:22 25 4
gpt4 key购买 nike

我想在我的数据框列中找到连续的 nans,比如

>>> df = pd.DataFrame([[np.nan, 2, np.nan],
... [3, 4, np.nan],
... [np.nan, np.nan, np.nan],
... [np.nan, 3, np.nan]],
... columns=list('ABC'))
>>> df
A B C
0 NaN 2.0 NaN
1 3.0 4.0 NaN
2 NaN NaN NaN
3 NaN 3.0 NaN

会给予

>>> df
A B C
0 1.0 NaN 4.0
1 NaN NaN 4.0
2 2.0 1.0 4.0
3 2.0 NaN 4.0

最佳答案

使用:

a = df.isnull()
b = a.ne(a.shift()).cumsum().apply(lambda x: x.map(x.value_counts())).where(a)
print (b)
A B C
0 1.0 NaN 4
1 NaN NaN 4
2 2.0 1.0 4
3 2.0 NaN 4

详细信息:

#unique consecutive values
print (a.ne(a.shift()).cumsum())
A B C
0 1 1 1
1 2 1 1
2 3 2 1
3 3 3 1

#count values per columns and map
print (a.ne(a.shift()).cumsum().apply(lambda x: x.map(x.value_counts())))
A B C
0 1 2 4
1 1 2 4
2 2 1 4
3 2 1 4

#add NaNs by mask a
print (a.ne(a.shift()).cumsum().apply(lambda x: x.map(x.value_counts())).where(a))
A B C
0 1.0 NaN 4
1 NaN NaN 4
2 2.0 1.0 4
3 2.0 NaN 4

一列替代:

a = df['A'].isnull()
b = a.ne(a.shift()).cumsum()
c = b.map(b.value_counts()).where(a)

print (c)
0 1.0
1 NaN
2 2.0
3 2.0
Name: A, dtype: float64

关于python - 在 Pandas 数据框中查找连续的 Nans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47661565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com