【问题标题】:get value from data that depend on other data从依赖于其他数据的数据中获取价值
【发布时间】:2018-10-28 11:52:06
【问题描述】:

我正在尝试获取介于 0,1 之间的 l 输入。 l 输入将用于 A 列。第二个输入将是“mesafe”列,因此结果必须为 23,即 A 和 mesafe 的零列。我遇到了一些错误。

import pandas as pd
import numpy as np

def var():
    df = pd.read_csv('l_y.txt')
    l=float(input("speed of the wind:"))
    w=int(input("range:"))
    for l in range(0, 1) and w in range(0, 100) :
        print(df['A'].values[0])




l_y.txt=(  mesafe      A     B     C     D     E     F
       0     100     23    18    14     8     4     0
       1    1000    210   170   110    60    40    30
       2    5000    820   510   380   300   230   160
       3   10000   1600  1200   820   560   400   250
       4   20000   2800  2100  1600  1000   820   500
       5   50000   5900  4600  3400  3200  1600  1100
       6  100000  10000  8100  6100  3900  2800  2000 )    






Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    var()
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\ml.py", line 
8, in var
    for l in range(0, 1) and w in range(0, 100) :
TypeError: 'bool' object is not iterable

【问题讨论】:

  • 我认为您可能想尝试将一个 for 循环嵌套在另一个循环中。
  • 我试过了:关键错误'A'
  • 我很抱歉我的英语不好。是的,就像你说的 l_y 给予取决于什么是用户输入。我有一个公式,在这个公式中有一个取决于风速和范围的变量,“mesafe”平均值是范围,其他列是速度,范围的值类似于 0-1、1 -5, 5-12...
  • 因此对于风速和范围的每个值都有一个特殊的值 l_y。希望你能理解
  • 好吧,我想我开始明白了。但是如果 l 到字母 A...F 的映射不是 0, 1, 2 ...6 而是 0-1, 1-5, 5-12... 我们需要知道这个列表的其余部分

标签: python pandas


【解决方案1】:

您必须遵循语言的格式。在这种情况下,如果您想使用lw 运行双循环:

for l in range(0, 1):
    for w in range(0, 100) :
        print(df['A'].values[0])

注意缩进,如果你没有正确缩进,那么python代码将无法正常工作。

此外,您的代码似乎没有完成任何事情,除了打印相同的东西 200 次。你想做什么?

【讨论】:

  • 该代码的输出将用于核动力反应堆事故中锶90的测定。在物理学中有很多因素,因此数据是这些因素之一,我想获得的值取决于其他列的值。
【解决方案2】:

好的,我假设您想从矩阵中获取某个值,具体取决于两个输入值 l 和 w,因此如果 l 介于 0 和 1 之间,则应选择列“A”。 (我进一步假设,如果 l 在 1 nd 2 之间,它是列 'B',2 'c',依此类推......) 该行直接从 w 与 mesafe-column 中的数据派生:如果 w 在 0 和 100 之间 -> 第 0 行,在 100 和 1000 之间 -> 第 1 行等等...

嗯,可以这样实现:

l = .3   # let's say user types 0.3

l 和字母之间有一些映射:

l_mapping = [1, 5, 12, 20, 35, 50]    # These are the thresholds between the columns A...F

l_index = np.searchsorted(l_mapping, l)   # calculate the index of the column letter

col = df.columns[1:][l_index]    # this translates l to the column names from A...F

col     # look what col is when l was < 1
Out: 'A'

w = 42   # now the user Input for w is 42

row = np.searchsorted(df.mesafe.values, w)   # this calculates the fractional index of w in df.mesafe

row
Out: 0

因此,通过这两个公式,您可以获得列和行信息来索引您想要的结果:

df[col].iloc[row]
Out: 23

在一个函数中总结这一切看起来像这样:

def get_l_y(l, w, df_ly):
    l_mapping = [1, 5, 12, 20, 35, 50]
    l_index = np.searchsorted(l_mapping, l)
    col = df_ly.columns[1:][l_index]
    row = np.searchsorted(df.mesafe.values, w)
    print(l, w, col, row)    # print Input and calculated row- and column- values for testing purpose, can be deleted/commented out if everything works as you want 
    return df[col].iloc[row]

此函数需要 l、w 和矩阵的 pandas-dataframe 作为输入参数并返回 l_y。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-06
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    相关资源
    最近更新 更多