在df['Stem']列中,我想找到每行字符数最少的单词,并将该值放入df['lab']
电流输出
group stem lab
----------------- --------- --------------------- -------------
notif notify, notified nan
face-to-fac face-to-face nan
propo proposed nan
lift lifted nan
govern governed, governing, government, governance nan
constitut constitute, constitutional nan
exp expedient nan
prev preventing, prevent nan
tre treat, treatment nan
work working, works nan
预期产量
group stem lab
----------------- --------- --------------------- -------------
notif notify, notified notify
face-to-fac face-to-face face-to-face
propo proposed proposed
lift lifted lifted
govern governed, governing, government, governance governed
constitut constitute, constitutional constitute
exp expedient expedient
prev preventing, prevent prevent
tre treat, treatment treat
work working, works works
尝试
df['lab'] = df.stem.str.split(',').apply(lambda x: min(x, key=len))
但它的给予
TypeError: 'float' object is not iterable
您可以使用^{} 从内置函数设置} 中的字符串获取字符串列表
stem
和^{min
,以便它返回最短的字符串:某些行的结果:
相关问题 更多 >
编程相关推荐