python - 自动查找非 nan 值及其对应的索引点

标签 python arrays indexing

根据与合法值(而非 NaN)相对应的索引号自动查找不同点的值,因为根据我放置在初始数据上的函数,整个数据中会有许多 NaN。

我有一个 DataFrame(名为“future”),我在其中选择了整个(743 个初始行)的相对最小值/最大值的特定点,并且能够将这些最小值/最大值的索引点放入数组并将它们添加到 ' graph' dataframe ('closemin', 'closemax', 'rsimin', 'rsimax')数组的值由'graph' DataFrame 中它们各自列中的这些最小值/最大值的索引点组成。

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

最佳答案

当您遍历这些行时,您需要引用适用于两个数据帧的公共(public)索引。在我这里的示例中,我有两个具有不同数据但引用相同索引的数据框。假设一个数据帧引用关闭数据,另一个引用关闭最小数据。

它是这样工作的:

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]
----

关于python - 自动查找非 nan 值及其对应的索引点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55105175/

相关文章:

python - NumPy:如何过滤矩阵行

python - 如何在Python中创建一个包含多个数组的单个数组?

PHP 循环并重建按值(主题)输出 JSON 分组的数组

python - 字符串列表到整数数组

python - 从命令行调用 bash 脚本和通过 python 执行它之间的区别

python - 处理 100 万行键的 pycassa multiget 的有效方法是什么

mysql - 一起声明索引和单独声明索引有什么区别?

mysql - 我的 SQL 查询太慢以至于超时。另一个解决方案?

python - 更改多索引数据帧较低级别中的多个值

Python:如何获取字典中可能的键组合