简体   繁体   English

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

[英]passing dataframe to a function in python

I have written a function in python , but I am passing string to a function as a parameter , but I have a excel file that is Dataframe which has many rows now i want to process each row of a column as a string .How do i do that ? 我已经在python中编写了一个function ,但我将string作为parameter传递给function ,但是我有一个excel文件是Dataframe,它有很多行,现在我想将列的每一行作为string我怎么做去做 ?

I have written the following function which takes the string as a input no wi want o pass dataframe to the function , how do i do that? 我编写了以下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 looks like this 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

I have this kind of dataframe. 我有这种数据帧。 I want to using utterance column as a string in the above function 我想在上面的函数中使用话语列作为字符串

See a mockup. 看一个样机。 basically you are updating a dataframe column with the logic in the function ( remove_numbers : this remove all numbers from the utterance column). 基本上你是用函数中的逻辑更新数据帧列( remove_numbers :这将从话语列中删除所有数字)。 Let me know if it works. 如果有效,请告诉我。

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

result below: 结果如下:

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.

相关问题 将 dataframe 的行作为参数传递给 python 中的 function - passing rows of dataframe as argument to a function in python 在函数中传递数据框和变量 - passing dataframe and variables in function 在python中创建Dataframe,将其作为参数传递给R中的函数,但是dataframe列不可访问。 使用RPy2 - Creating Dataframe in python, passing it as parameter to function in R, but the dataframe columns aren't accessable. Using RPy2 将 python 中的缓存 pandas dataframe 传递给另一个缓存的 ZC1C425268E68385D1AB5074unhashable 类型错误“A:A:941AB5074unhash7” - Passing cached pandas dataframe in python to another cached function give "unhashable type: dataFrame" error 通过将其他数据框的列值和标量传递给Pandas Python中的函数,在第二个数据框中创建新列? - Create a new column in a second dataframe by passing column values of a different dataframe and a scalar to a function in Pandas Python? 将 Pandas 数据帧对象传递给函数 - Passing a pandas dataframe object into a function 命名数据框将其作为函数中的参数传递 - Naming dataframe passing it as argument in a function 分组后将数据框传递给函数 - passing dataframe to function after Group by 在函数中将运算符传递给 pandas 数据框: - passing operators to pandas dataframe in function: 在python中传递可选的dataframe参数 - passing optional dataframe parameter in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM