自动查找nonnan值及其对应的索引点

2024-09-30 12:15:53 发布

您现在位置:Python中文网/ 问答频道 /正文

根据与合法值(而不是nan)对应的索引号自动查找不同点上的值,因为根据我在初始数据上放置的函数,整个数据中将有许多nan。你知道吗

我有一个数据帧(名为‘future’),在这里我挑选出了贯穿始终的相对最小值/最大值的特定点(743个初始行),并且能够将这些最小值/最大值的索引点放入数组中,并将它们添加到‘graph’数据帧(‘closemin’、‘closemax’、‘rsimin’,“rsimax”)数组的值由这些最小/最大值的索引点组成,这些索引点位于“graph”数据框中各自的列中。你知道吗

我试图找到相对接近的最小值/最大值之间的斜率,然后将其与相同索引点处RSIE14的斜率进行比较。我可以很容易地找到索引点,但是没有一种方法来自动化这个过程——这是我对其他数据集所需要的,因为这些相对最小/最大点之间的NaN值经常会改变。 例如,在下图中,索引号351和340处有相对的“closemin”。我想自动获取这些索引点,然后同时为RSIE14数据获取相同的索引点(351和340),这样我就可以自动找到两者的斜率。 Pic showing dataframe and array outputs


Tags: 数据函数future数组nan中将graph合法
1条回答
网友
1楼 · 发布于 2024-09-30 12:15:53

当您在这些行中循环时,您需要引用一个应用于两个数据帧的公共索引。在这里的示例中,我有两个数据帧,它们的数据不同,但引用的索引相同。假设一个dataframe引用close数据,另一个引用closemin数据。你知道吗

这就是它的工作原理:

import pandas as pd
import random

my_randoms = [random.sample(range(100), 10), random.sample(range(100), 10)]
my_other_randoms = [random.sample(range(100), 10), random.sample(range(100), 10)]

first_dataframe = pd.DataFrame(my_randoms).T
second_dataframe = pd.DataFrame(my_other_randoms).T

print(first_dataframe)
print("  ")
print(second_dataframe)
print("  ")

for index, row in first_dataframe.iterrows():
    print(f"Index of current row: {index} \n"
          f"Values of current row: {row.values}\n"
          f"Values on same row other DF: {second_dataframe.iloc[index].values}\n"
          f"  ")

带输出:

    0   1
0  90  61
1  99  88
2  15  56
3  17  37
4  95  93
5  23  43
6  68  14
7   7   9
8  97   2
9  53  91
  
    0   1
0   6  88
1  21  51
2   2  50
3  38  40
4  11  67
5  57  80
6   9  41
7  88  47
8  41  72
9  42  52
  
Index of current row: 0 
Values of current row: [90 61]
Values on same row other DF: [ 6 88]
  
Index of current row: 1 
Values of current row: [99 88]
Values on same row other DF: [21 51]
  
Index of current row: 2 
Values of current row: [15 56]
Values on same row other DF: [ 2 50]
  
Index of current row: 3 
Values of current row: [17 37]
Values on same row other DF: [38 40]
  
Index of current row: 4 
Values of current row: [95 93]
Values on same row other DF: [11 67]
  
Index of current row: 5 
Values of current row: [23 43]
Values on same row other DF: [57 80]
  
Index of current row: 6 
Values of current row: [68 14]
Values on same row other DF: [ 9 41]
  
Index of current row: 7 
Values of current row: [7 9]
Values on same row other DF: [88 47]
  
Index of current row: 8 
Values of current row: [97  2]
Values on same row other DF: [41 72]
  
Index of current row: 9 
Values of current row: [53 91]
Values on same row other DF: [42 52]
  

相关问题 更多 >

    热门问题