忽略NaN的变化来计算 pandas 中数据框中每一列的值变化 [英] Counting changes of value in each column in a data frame in pandas ignoring NaN changes

查看:90
本文介绍了忽略NaN的变化来计算 pandas 中数据框中每一列的值变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试统计以熊猫为单位的数据框中每一列的值变化次数.除NaN之外,我拥有的代码非常有效:如果一列包含两个后续的NaN,则将其视为值的更改,这是我不希望的.我该如何避免呢?

I am trying to count the number of changes of value in each column in a data frame in pandas. The code I have works great except for NaNs: if a column contains two subsequent NaNs, it is counted as a change of value, which I don't want. How can I avoid that?

我这样做如下(感谢 unutbu的回答):

I do as follows (thanks to 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

相反,结果应该是(请注意时间列中的更改):

Instead, the results should be (notice the change in the time column):

time    2
X1      3
X2      2
dtype: int64

推荐答案

change = (frame.fillna(0).diff() != 0).sum()

输出:

time    2
X1      3
X2      2
dtype: int64

NaN是真实的" .将NaN更改为零,然后求值.

NaN are "truthy". Change NaN to zero then evaluate.

nan - nan = nan

nan != 0  = True

fillna(0)

0 - 0 = 0

0 != 0 = False

这篇关于忽略NaN的变化来计算 pandas 中数据框中每一列的值变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆