有没有办法将字符串变量传递到 python .loc/.iloc?

Is there a way to pass a string variable into python .loc/.iloc?

我正在尝试使用 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 的属性,所以它不起作用。

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

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

Pandas Query Documentation

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.