如何使用字符串作为python函数语法?
如何使用字符串作为python函数语法?,python,string,function,dataframe,filter,Python,String,Function,Dataframe,Filter,dfm是函数中的数据帧参数。我需要将col\u string指定为数据帧中列名的字符串,但是当我执行过滤时,函数将col\u string作为字符串而不是实际的函数语法读取 def the_function(dfm, col_string, filt_string): filtered_df = dfm[dfm.col_string == filt_string] print(filtered_df) the_function(df, 'Name', 'Zero') 对于变量
dfm
是函数中的数据帧参数。我需要将col\u string
指定为数据帧中列名的字符串,但是当我执行过滤时,函数将col\u string
作为字符串而不是实际的函数语法读取
def the_function(dfm, col_string, filt_string):
filtered_df = dfm[dfm.col_string == filt_string]
print(filtered_df)
the_function(df, 'Name', 'Zero')
对于变量,您需要使用括号表示法
filtered_df = dfm[dfm[col_string] == filt_string]
print(filtered_df)
请显示您的代码,以便我们能够正确评估。请查看并相应地在您的帖子中进行编辑。