查找列中字符数最少的值

2024-06-06 23:40:30 发布

您现在位置:Python中文网/ 问答频道 /正文

在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

Tags: todflabnotifynanfaceworksstem
1条回答
网友
1楼 · 发布于 2024-06-06 23:40:30

您可以使用^{}从内置函数设置stem^{}中的字符串获取字符串列表min,以便它返回最短的字符串:

df['lab'] = df.stem.str.split(',').apply(lambda x: min(x, key=len))

某些行的结果:

        group                                            stem         lab
0       notif                                notify, notified      notify
1  ace-to-fac                                      ace-to-fac  ace-to-fac
2      govern     governed, governing, government, governance    governed

相关问题 更多 >