繁体   English   中英

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

[英]passing dataframe to a function in python

我已经在python中编写了一个function ,但我将string作为parameter传递给function ,但是我有一个excel文件是Dataframe,它有很多行,现在我想将列的每一行作为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()

Dataframe看起来像这样

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

我有这种数据帧。 我想在上面的函数中使用话语列作为字符串

看一个样机。 基本上你是用函数中的逻辑更新数据帧列( 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM