在Column pandas中查找字符最少的值

时间:2019-04-17 15:45:11

标签: python pandas

我要在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

1 个答案:

答案 0 :(得分:4)

您可以使用str.splitstemapply内置min函数设置key=len中的字符串中获取字符串列表,以便返回最短的字符串:

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