我正在尝试计算pandas中数据框中每列中值的更改次数。除了NaN之外,我的代码工作得很好:如果一个列包含两个后续的NaN,它将被视为值的变化,这是我不想要的。我怎么能避免这种情况?
我做如下(感谢unutbu's answer):
import pandas as pd
import numpy as np
frame = pd.DataFrame({
'time':[1234567000 , np.NaN, np.NaN],
'X1':[96.32,96.01,96.05],
'X2':[23.88,23.96,23.96]
},columns=['time','X1','X2'])
print(frame)
changes = (frame.diff(axis=0) != 0).sum(axis=0)
print(changes)
changes = (frame != frame.shift(axis=0)).sum(axis=0)
print(changes)
返回:
time X1 X2
0 1.234567e+09 96.32 23.88
1 NaN 96.01 23.96
2 NaN 96.05 23.96
time 3
X1 3
X2 2
dtype: int64
time 3
X1 3
X2 2
dtype: int64
相反,结果应该是(注意时间列的变化):
time 2
X1 3
X2 2
dtype: int64
答案 0 :(得分:3)
change = (frame.fillna(0).diff() != 0).sum()
输出:
time 2
X1 3
X2 2
dtype: int64
NaN是"truthy"。将NaN更改为零然后评估。
nan - nan = nan
nan != 0 = True
fillna(0)
0 - 0 = 0
0 != 0 = False