将函数(带有两个参数)一次应用于DataFrame的两列

时间:2018-07-06 17:29:15

标签: python pandas dataframe

考虑到该函数接受两个参数,如何在数据框的元素两列上使用下面显示的函数?我数据框的每一行都是List格式,每列中元素的数量已经相同;我只需要帮助应用此功能。我已经尝试过.apply()方法,但无法成功。非常感谢。

def tagged(word, tag):
   final = [x + '|' + y for x in word for y in tag]
   return final


#testing method:
tagged(['a', 'b'], ['1', '2'])

#Output:
['a|1','a|2','a|3','a|4','b|1','b|2','b|3','b|4','c|1','c|2']

#Sample of my Dataframe
import pandas as pd
df = pd.DataFrame({'text':['I','am'], 'tag': ['PRN','ADJ']})

1 个答案:

答案 0 :(得分:1)

怎么样?

>>> df['result'] = [tagged(w, t) for w, t in df[['word col', 'tag col']].values)]
相关问题