Python 3.x 在数据帧上使用lambda、apply和join函数
目标 将Python 3.x 在数据帧上使用lambda、apply和join函数,python-3.x,pandas,join,lambda,apply,Python 3.x,Pandas,Join,Lambda,Apply,目标 将deid\u notes函数应用于df import pandas as pd df = pd.DataFrame({'Text' : ['there are many different types of crayons', 'i like a lot of sports cares', 'the middle east has many ca
deid\u notes
函数应用于df
import pandas as pd
df = pd.DataFrame({'Text' : ['there are many different types of crayons',
'i like a lot of sports cares',
'the middle east has many camels '],
'P_ID': [1,2,3],
'Word' : ['crayons', 'cars', 'camels'],
'P_Name' : ['John', 'Mary', 'Jacob'],
'N_ID' : ['A1', 'A2', 'A3']
})
#rearrange columns
df = df[['Text','N_ID', 'P_ID', 'P_Name', 'Word']]
df
Text N_ID P_ID P_Name Word
0 many types of crayons A1 1 John crayons
1 i like sports cars A2 2 Mary cars
2 has many camels A3 3 Jacob camels
背景
我有一个类似于此示例的df
import pandas as pd
df = pd.DataFrame({'Text' : ['there are many different types of crayons',
'i like a lot of sports cares',
'the middle east has many camels '],
'P_ID': [1,2,3],
'Word' : ['crayons', 'cars', 'camels'],
'P_Name' : ['John', 'Mary', 'Jacob'],
'N_ID' : ['A1', 'A2', 'A3']
})
#rearrange columns
df = df[['Text','N_ID', 'P_ID', 'P_Name', 'Word']]
df
Text N_ID P_ID P_Name Word
0 many types of crayons A1 1 John crayons
1 i like sports cars A2 2 Mary cars
2 has many camels A3 3 Jacob camels
我使用以下函数使用NeuroNER来识别文本
列中的某些单词
问题
我使用以下代码将deid_notes
功能应用于我的df
fx = lambda x: deid_notes(x.Text,axis=1)
df.join(df.apply(fx))
但是我得到了以下错误
AttributeError: ("'Series' object has no attribute 'Text'", 'occurred at index Text')
问题
如何让
deid_notes
函数在我的df
上工作?假设您从deid_notes
函数返回一个熊猫系列作为输出,并将text
作为唯一的输入参数。将axis=1
参数传递给apply
而不是dead\u notes
。例如
# Dummy function
def deid_notes(text):
deid = 'prediction to: ' + text
return pd.Series(deid, index = ['Deid'])
fx = lambda x: deid_notes(x.Text)
df.join(df.apply(fx, axis =1))
在这种情况下,
n1
是什么?n1=neuromodel.NeuroNER(train\u model=False,use\u pretrained\u model=True,dataset\u text\u folder=“./data/example\u unnotated\u text”,pretrained\u model\u folder=“/trained\u models/mimic\u globle\u stanford\u bios”)
Trydf.join(df.apply(fx,axis=1))
我得到一个错误TypeError:(“deid_notes()得到一个意外的关键字参数'axis',发生在索引0')