python函数中的参数

python函数中的参数,python,pandas,Python,Pandas,我试图解决两个函数关于参数的问题。 具体而言,我有以下问题: def remove_stopwords(text): list_words=[] stop_words = (stopwords.words('italian')) #bla bla bla return(' '.join(t)) def clean(file): # bla bla bla file['C'] = file['Text'].apply(remove_stopwords()

我试图解决两个函数关于参数的问题。 具体而言,我有以下问题:

def remove_stopwords(text):
list_words=[]
stop_words = (stopwords.words('italian'))
                  
#bla bla bla
return(' '.join(t))

def clean(file):


# bla bla bla
file['C'] = file['Text'].apply(remove_stopwords())
# bla bla bla
return
然后当我调用函数时,如下所示:

clean(df)
它工作得很好。 但是,我想这样做:

language='italian' 
clean(df, language)
def remove_stopwords(text, language):
    list_words=[]
    stop_words = (stopwords.words(language))               
    return(' '.join(t))

def clean(file, language):
    file['C'] = file['Text'].apply(lambda x: remove_stopwords(x, language))
    return file['C']

clean(file, 'italian')
其中,语言应该是要放置在此处的字符串:

def remove_stopwords(text):

    list_words=[]
    stop_words = (stopwords.words(str(language)) # <--
...
def remove_stopwords(文本):
列出单词=[]

stop_words=(stopwords.words(str(language))#您必须在函数中传递参数才能在函数中使用它, 但您尚未在函数定义中定义第二个参数

试试这个:

language='italian' 
def remove_stopwords(text, language):
    list_words=[]
    stop_words = (stopwords.words(language)
    .....

您试图给出两个参数,而您只指定了一个。您需要这样做

def clean(df=None, language=None):
    # Your code

为了能够给出两个参数,您必须告诉函数将有两个参数。

我相信lambda函数将完成您正在尝试的操作。类似于以下内容:

language='italian' 
clean(df, language)
def remove_stopwords(text, language):
    list_words=[]
    stop_words = (stopwords.words(language))               
    return(' '.join(t))

def clean(file, language):
    file['C'] = file['Text'].apply(lambda x: remove_stopwords(x, language))
    return file['C']

clean(file, 'italian')

我试过了,但我得到了这个错误:
TypeError:remove\u stopwords()缺少两个必需的位置参数:“text”和“language”
。在
stop\u words=(stopwords.words('意大利语'))
中,我应该有类似于
stop\u words=(stopwords.words(str(language))
您似乎没有同时传入两个参数。要使传入参数成为可选,请执行此操作。(我将编辑新代码)clean呢?我收到以下错误:
TypeError:clean()缺少1个必需的位置参数:“language”
@LucaDiMauro您需要在干净的函数定义中定义第二个参数,如remove\u stopwords函数,它会工作的。非常感谢@jjordan。您的回答非常有用