Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用给定值比较数据帧列?_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何使用给定值比较数据帧列?

Python 如何使用给定值比较数据帧列?,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个如下所示的数据帧: >>> df = pd.DataFrame( {'InLevel_03': [12, 12, 13, 12, 11,], 'InLevel_02': [11.5, 11.5, 12.5, 11.5, 10.5], 'InLevel_01': [11, 10.5, 12, 10.5, 9], 'OutLevel_01': [10.5, 10, 11.5, 10, 8.5], 'OutLevel_02': [10, 9.5, 11, 9.5, 8], '

我有一个如下所示的数据帧:

>>> df = pd.DataFrame( {'InLevel_03': [12, 12, 13, 12, 11,], 'InLevel_02': [11.5, 11.5, 12.5, 11.5, 10.5], 'InLevel_01': [11, 10.5, 12, 10.5, 9], 'OutLevel_01': [10.5, 10, 11.5, 10, 8.5], 'OutLevel_02': [10, 9.5, 11, 9.5, 8], 'OutLevel_03': [9.5, 9, 10, 9, 7.5]} )

>>> df
   InLevel_03  InLevel_02  InLevel_01  OutLevel_01  OutLevel_02  OutLevel_03
0          12        11.5        11.0         10.5         10.0          9.5
1          12        11.5        10.5         10.0          9.5          9.0
2          13        12.5        12.0         11.5         11.0         10.0
3          12        11.5        10.5         10.0          9.5          9.0
4          11        10.5         9.0          8.5          8.0          7.5
 gapLevel    count    # row number, column name of each gap
       11        2    # (1, InLevel_02 - 1, InLevel_01), (3, InLevel_02 - 3, InLevel_01)
     10.5        1    # (2, OutLevel_02 - 2, OutLevel_03)
       10        1    # (4, InLevel_02 - 4, InLevel_01)
      9.5        1    # (4, InLevel_02 - 4, InLevel_01)
如果给定值为
0.5
,我想检查一行中是否有大于给定值的间隙。例如,在第二行中,InLevel_02(11.5)和InLevel_01(10.5)之间有一个间隙,即11。在第5行中,InLevel_02(10.5)和InLevel_01(9.0)之间的间隙分别为10和9.5

此作业的结果如下所示:

>>> df = pd.DataFrame( {'InLevel_03': [12, 12, 13, 12, 11,], 'InLevel_02': [11.5, 11.5, 12.5, 11.5, 10.5], 'InLevel_01': [11, 10.5, 12, 10.5, 9], 'OutLevel_01': [10.5, 10, 11.5, 10, 8.5], 'OutLevel_02': [10, 9.5, 11, 9.5, 8], 'OutLevel_03': [9.5, 9, 10, 9, 7.5]} )

>>> df
   InLevel_03  InLevel_02  InLevel_01  OutLevel_01  OutLevel_02  OutLevel_03
0          12        11.5        11.0         10.5         10.0          9.5
1          12        11.5        10.5         10.0          9.5          9.0
2          13        12.5        12.0         11.5         11.0         10.0
3          12        11.5        10.5         10.0          9.5          9.0
4          11        10.5         9.0          8.5          8.0          7.5
 gapLevel    count    # row number, column name of each gap
       11        2    # (1, InLevel_02 - 1, InLevel_01), (3, InLevel_02 - 3, InLevel_01)
     10.5        1    # (2, OutLevel_02 - 2, OutLevel_03)
       10        1    # (4, InLevel_02 - 4, InLevel_01)
      9.5        1    # (4, InLevel_02 - 4, InLevel_01)
我尝试将数据帧转换为数组(使用.to_记录)并使用循环将每个值与其下一个值进行比较,但当两个值之间的级别超过1级时,代码会变得太复杂,我想知道是否有更有效的方法来实现这一点。

这里有一种方法:

您可以从获取行和列的索引列表开始,从中提取计数检查
df
减去自身的移位版本(请参阅)大于
0.5

t = 0.5
# df = df.astype(float) # if it isn't already
rows, cols = np.where(df - df.shift(-1, axis = 1) > t)
# (array([1, 2, 3, 4]), array([1, 4, 1, 1]))
使用列表理解as从这些行和列中的值中获取arange(注意,这种方法假设值在整个列中不断减少):

使用
计数器从计数创建新的
系列

from itertools import chain
from collections import Counter

x = list(chain.from_iterable(v.values))
#[11.0, 10.5, 11.0, 9.5, 10.0]
pd.Series(Counter(x), name = 'count')

11.0    2
10.5    1
9.5     1
10.0    1
Name: count, dtype: int64

理解需要做什么…但是你能解释更多关于你的输出df吗?
gapLevel
如何与输入
df
中的值匹配?您是否只希望在输出df中使用这两列,即
gapLevel
count
。您不想在orginial'df'@Rahul Agarwal中合并这些列,谢谢您的回复。我补充了结果是如何产生的。我只是计算了原始数据帧中每个级别缺少的间隙。我认为结果应该在一个新的数据框中完成,因为它的形状不同。因此,间隙是一行中的值之间的间隙,是该行中的任何值之间的间隙,还是仅相邻的值对之间的间隙?@cardamom我很抱歉我的问题不够清晰。我指的是坐在一起的一对。您可以看到第一行中的级别减少了0.5
(12、11.5、11.0、10.5、10.0、9.5)。
如果不是减少了0.5,而是减少了1(存在1个间隙)或1.5(存在2个间隙),那么它不是连续的,并且存在间隙。感谢您的帮助!它工作得很好,我学到了一些新的方法来处理数据。但是,使用my
(610178,10)
形状的数据帧执行
[np.arange(*df.iloc[r[c+1,c]].values,step=t)[1:]对于zip中的r,c(行,列)]