<p>假设我有这个数据帧<strong>df</strong>:</p>
<pre><code>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
</code></pre>
<p>如何获取<strong>第2列</strong>中每行所有字符(不包括空格)的总和,并将其返回到新的<strong>第4列</strong>,如下所示:</p>
<pre><code>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
</code></pre>
<p>我尝试了这段代码,但到目前为止,我得到了column2中每行所有字符的总和:</p>
<pre><code>df['column4'] = sum(list(map(lambda x : sum(len(y) for y in x.split()), df['column2'])))
</code></pre>
<p>因此,当前我的<strong>df</strong>如下所示:</p>
<pre><code>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
</code></pre>
<p>有人知道吗</p>