我有一个数据框df1
,如下所示:
date sentence
29/03/1029 i like you
.....
我想像这样创建新的数据框df2
:
date verb object
29/03/2019 like you
....
具有以下功能:
def getSplit(df1):
verbList = []
objList = []
df2 = pd.DataFrame()
for row in df1['sentence']:
verb = getVerb(row)
obj = getObj(row)
verbList.append(verb)
objList.append(obj)
df2 = df1[[date]].copy
df2['verb'] = verbList
df2['object'] = objList
return df2
我的功能运行良好,但是速度很慢。有人可以帮我改善功能,使其运行更快吗?
谢谢
答案 0 :(得分:3)
您可以使用熊猫的apply
方法来快速处理:-
getverb(row):
pass # Your function
getobj(row):
passs # Your function
df2 = df1.copy() # Making copy of your dataframe.
df2['verb'] = df2['sentence'].apply(getverb)
df2['obj'] = df2['sentence'].apply(getobj)
df2.drop('sentence', axis=1, inplace=True) # Droping sentence column
df2
我希望它可以对您有所帮助。 (接受并认可答案)