Python 将行中所有字符的总和返回到另一列
假设我有这个数据帧df:Python 将行中所有字符的总和返回到另一列,python,pandas,Python,Pandas,假设我有这个数据帧df: column1 column2 column3 amsterdam school yeah right backtic escapes sport swimming 2016 rotterdam nope yeah 2012 thehague i now i can fl
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
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
我尝试了这段代码,但到目前为止,我得到了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 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
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()))
或者获取所有值的计数,并将空白的计数减去:
嗨,这对我有用
import pandas as pd
df=pd.DataFrame({'col1':['Stack Overflow','The Guy']})
df['Count Of Chars']=df['col1'].str.replace(" ","").apply(len)
df
输出
col1 Count Of characters
0 Stack Overflow 13
1 The Guy 6
您可以将方法
count
与正则表达式模式一起使用:
df['column2'].str.count(pat='\w')
输出:
0 42
1 8
2 34
3 30
Name: column2, dtype: int64
第二个很聪明:)+1就像一个咒语@jezrael。非常感谢。有什么有趣的链接可以让我阅读并深入了解python中的lambda吗?非常感谢。again@JackZakiZakiulFahmiJailani-你可以检查@jezrael,你可以让它更简单。请检查我的答案。您可能希望更改预期输出,因为它具有误导性。似乎不对