【问题标题】:Is there a way to pass a string variable into python .loc/.iloc?有没有办法将字符串变量传递给 python .loc/.iloc?
【发布时间】:2022-01-07 08:50:38
【问题描述】:

我试图使用 loc 在条件下获取数据框中的行子集,但我想获取用户输入以获取此条件,然后将其输入 loc 语句以创建行子集。

我尝试了很多方法,但我认为 loc 不会接受这种格式的字符串中的条件,有没有办法解决这个问题?

请参阅下面的尝试:

col_one = input("Please enter the condition you would like to set. E.g. State == "New York":)

user_input_test.append(col_one)
one_condition_input = self.df.loc[self.df[user_input_test],:]


# I also tried to use slice but no luck:
col_one = input("Please enter the condition you would like to set. E.g. State == "New York":)
period = slice(col_one)
self.one_condition_input = self.df.loc[period,:]


# And I tired to use format, taking two user inputs, one with column name and one with the condition, but again no luck:
col_one = input("Please enter the column you would like to set. E.g. State":)
col_two = input("Please enter the condition you would like to set. E.g. == "New York":)
one_condition_input = self.df.loc[self.df["{}".format(col_one)]"{}".format(col_two),:]

我希望能够获取整个条件的用户输入并将其粘贴如下:

col_one = input("Please enter the condition you would like to set. E.g. State == "New York":)
self.one_condition_input = self.df.loc[df.col_one,:]

但显然这里 col_one 不是 df 的属性,因此不起作用。

【问题讨论】:

    标签: python pandas string user-input pandas-loc


    【解决方案1】:

     DataFrame.loc 属性: 通过标签或 boolean array 访问一组行和列。

     DataFrame.iloc 属性:纯粹基于整数位置的索引,用于按位置进行选择。

    实际上这些接受一个值作为文本字符串以将其索引到相应的列,我建议您使用用户输入但对这些值执行条件

    user_input_test.append(col_one)
    one_condition_input = df.loc[df[user_input_test],:]
    

    改为:

    user_input_test.append(col_one)
    cond = re.findall(r'\w+', user_input)
    col = cond[0]
    col_element = " ".join(cond[1:])
    one_condition_input = df.loc[df[col == col_element],:]
    .
    .
    .
    >>> user_input = "State == New York" # User input value
    >>> cond = re.findall(r'\w+', user_input) # Separate strings
    ['State', 'New', 'York']
    >>> # This is equivalent to df.loc[df["State" == "New York"]]
    >>> one_condition_input = df.loc[df[col == col_element],:] # Values correspoding to columns containing "New York" state.
    

    【讨论】:

      【解决方案2】:

      试试pandas.DataFrame.query(),你可以传递一个表达式。因此,您可以要求用户插入表达式,然后将其传递给函数。

      expr = input()
      df.query(expr, inplace = True)
      

      Pandas Query Documentation

      【讨论】:

      • 正是我所需要的。太棒了,谢谢!
      猜你喜欢
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 2014-10-07
      • 2018-10-30
      • 2018-07-03
      相关资源
      最近更新 更多