将一行中所有字符的总和返回到另一列pandas - python

假设我有这个数据框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

如何获取column2中每一行的所有字符的总和(不包括空格),然后将其返回到新的column4中,如下所示:

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

有人有主意吗?

参考方案

在解决方案中使用自定义lambda函数:

df['column4'] = df['column2'].apply(lambda x: sum(len(y) for y in x.split()))

或获取所有值的计数并通过Series.str.count减去空格计数:

df['column4'] = df['column2'].str.len().sub(df['column2'].str.count(' '))
#rewritten to custom functon
#df['column4'] = df['column2'].map(lambda x: len(x) - x.count(' '))
print (df)
     column1                                           column2  column3  \
0  amsterdam  school yeah right backtic escapes sport swimming     2016   
1  rotterdam                                         nope yeah     2012   
2   thehague       i now i can fly no you cannot swimming rope     2010   
3  amsterdam              sport cycling in the winter makes me     2019   

   column4  
0       42  
1        8  
2       34  
3       30  

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…