[英]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.