将数据帧传递给python中的函数

时间:2019-06-24 10:51:27

标签: python function dataframe

我已经用python编写了<androidx.recyclerview.widget.RecyclerView ,但是我将function作为string传递给了function,但是我有一个Excel文件,即Dataframe,它具有现在我想将多行处理为parameter的每一行。我该怎么做?

我写了下面的string,它不希望将字符串作为输入传递数据帧到function,我该怎么做?

function

数据框看起来像这样

def pre_process(utterance):
    utterance = remove_name(utterance)
    utterance = text_in_next_line_after_dot(utterance)
    utterance = convert_num_to_words(utterance)
    utterance = remove_stop_phrase(utterance)
    utterance = remove_character(utterance)
    utterance = remove_blank_lines(utterance)
return utterance.strip()

我有这种数据框。我想在上述函数中将话语列用作字符串

1 个答案:

答案 0 :(得分:0)

查看样机。基本上,您正在使用函数中的逻辑更新数据框列(remove_numbers:这将删除发声列中的所有数字)。让我知道它是否有效。

import pandas as pd
import re

df = pd.DataFrame({'id': [1,2,3,4],
                  'Utterance': [
                      'my name is cyley . I am at post91', 
                      'after 24 hours you need to send the email', 
                      ' there interaction id is 123456', 
                      'he is studying at masters school']})
def remove_numbers(s):
    return re.sub(r'\d+', '', s)



def pre_process():
    df['Utterance'] = df['Utterance'].apply(remove_numbers)
    #utterance = text_in_next_line_after_dot(utterance)
    #utterance = convert_num_to_words(utterance)
    #utterance = remove_stop_phrase(utterance)
    #utterance = remove_character(utterance)
    #utterance = remove_blank_lines(utterance)
    return None

pre_process()

df

结果如下:

Utterance   id
0   my name is cyley . I am at post 1
1   after hours you need to send the email  2
2   there interaction id is 3
3   he is studying at masters school    4