[英]Apply function to select specific words from each row of a dataframe
您能否給我一個提示如何使這項工作?
我制作了一個 function ,它應該根據 dataframe 的每一行的句子的第一個元素從每一行中選擇特定的單詞:
def distilerator(string):
'''Input: a dataframe row containing a sentence
Output: depending on the first word, return a specific selection'''
if string.str.contains("hello"):
return string.str.split('\\n')[0]
else:
return string.str.split('\\n')[0:2]
dataframe 是:
A B
hello\blah 456
123\foo\bar 678
而應用 function 我嘗試“應用”到一個名為“描述”的新列:
df['description'] = df['A'].apply(distilerator, axis=1) #axis =1 aims to apply to each row
所需的產品是 dataframe,如下所示:
A B description
hello\blah 456 hello
123\foo\bar 678 foo bar
我試圖盡可能地表達自己...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.