假设我有这个数据帧df:
column1 column2 column3
amsterdam school yeah right backtic escapes sport swimming 2016
rotterdam nope yeah 2012
thehague i now i can fly no you cannot swimming rope 2010
amsterdam sport cycling in the winter makes me 2019
如何获取第2列中每行所有字符(不包括空格)的总和,并将其返回到新的第4列,如下所示:
column1 column2 column3 column4
amsterdam school yeah right backtic escapes sport swimming 2016 70
rotterdam nope yeah 2012 8
thehague i now i can fly no you cannot swimming rope 2010 65
amsterdam sport cycling in the winter makes me 2019 55
我尝试了这段代码,但到目前为止,我得到了column2中每行所有字符的总和:
df['column4'] = sum(list(map(lambda x : sum(len(y) for y in x.split()), df['column2'])))
因此,当前我的df如下所示:
column1 column2 column3 column4
amsterdam school yeah right backtic escapes sport swimming 2016 250
rotterdam nope yeah 2012 250
thehague i now i can fly no you cannot swimming rope 2010 250
amsterdam sport cycling in the winter makes me 2019 250
有人知道吗
可以将方法
count
与正则表达式模式一起使用:输出:
嗨,这对我有用
输出
在解决方案中使用自定义lambda函数:
或者获取所有值的计数并用^{} 减去空白的计数:
相关问题 更多 >
编程相关推荐