Python 什么是检查熊猫数据框中多种趋势的最快方法?

Python 什么是检查熊猫数据框中多种趋势的最快方法?,python,pandas,dataframe,iteration,Python,Pandas,Dataframe,Iteration,我有一个25-30列的大数据框,数千行。 我需要分析一些列的趋势和一些列比率的趋势 现在我有3个选择: 1) 使用范围内的简单for i(len(df))逐行迭代,并为不同的列构建一系列if/else条件,将每个值与前面和后面的值进行比较 for i in range(len(df)): if (df.iloc[i]['col1'] < df.iloc[i]['col2']): print ('error type 1') if (df.iloc[i]['col

我有一个25-30列的大数据框,数千行。 我需要分析一些列的趋势和一些列比率的趋势

现在我有3个选择:

1) 使用范围内的简单for i(len(df))逐行迭代,并为不同的列构建一系列if/else条件,将每个值与前面和后面的值进行比较

for i in range(len(df)):
    if (df.iloc[i]['col1'] < df.iloc[i]['col2']):
      print ('error type 1')
    if (df.iloc[i]['col2'] < df.iloc[i]['col3']):
      print ('error type 2')
    if (df.iloc[i+1]['col1'] > 2 * df.iloc[i]['col1']):
      print ('error trend 1')
    if (df.iloc[i+1]['col2'] > 2 * df.iloc[i+1]['col2']):
      print ('error trend 2')
    if (df.iloc[i-1]['col2'] > 2 * df.iloc[i]['col2']):
      print ('error trend 2')
   # and so on, with around 40-50 if statements per line
编辑:示例:我有这个表:

   Index col1 col2 col3 col4 col5 col6 col7
   0     732    58   18  10    6    3    3
   1     754    60   18  10    6    3    3
   2     3964   365  98  34   34    17  13
   3     4286   417 110  36   35    19  15
   4     5807   545 155  54   53    27  21
   5     1681   132  46  16   13    9   8
   6     542    620  13  11    4    3   2
   7     319    38   30  20    4    2   2
   8     286    22   17  10    3    2   2
   9     324    25   18  10    3    2   2
   10    370    29   10   0    4    2   2
   11    299    28   19  10    3    2   2
   12    350    36   14  11    6    3   4
   13    309    34   14  11    7    3   4
在这无限小的数据部分,我想找出错误:

  • 当列中的值变为上一个和下一个值的2x或一半时(例如第2、5、6行)
  • 当连续列高于前一列时(如第6行中的col2>col1,第12行和第13行中的col7>col6,等等)
  • 对这些列进行大量的其他检查(比如col1/col2必须非常恒定,col2/col3必须相同,col6+col7必须小于col3,等等)
我的主要问题是用上一个和下一个值检查列比率。所有其他检查都很容易矢量化

对如何进行有什么建议吗


谢谢!

你可以添加样本数据吗?你可以直接执行
df['ratio12']/df['ratio12'].shift()
无需额外列你可以添加样本数据吗?你可以直接执行
df['ratio12']/df['ratio12'].shift()
无需额外列
   Index col1 col2 col3 col4 col5 col6 col7
   0     732    58   18  10    6    3    3
   1     754    60   18  10    6    3    3
   2     3964   365  98  34   34    17  13
   3     4286   417 110  36   35    19  15
   4     5807   545 155  54   53    27  21
   5     1681   132  46  16   13    9   8
   6     542    620  13  11    4    3   2
   7     319    38   30  20    4    2   2
   8     286    22   17  10    3    2   2
   9     324    25   18  10    3    2   2
   10    370    29   10   0    4    2   2
   11    299    28   19  10    3    2   2
   12    350    36   14  11    6    3   4
   13    309    34   14  11    7    3   4