Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算dataframe中列表长度的乘积并存储在新列中_Python_Pandas_Apply - Fatal编程技术网

Python 计算dataframe中列表长度的乘积并存储在新列中

Python 计算dataframe中列表长度的乘积并存储在新列中,python,pandas,apply,Python,Pandas,Apply,我有一个数据框,它的值是列表。如何计算一行中所有列表长度的乘积,并存储在单独的列中?也许下面的例子可以说明这一点: test_1 = ['Protocol', 'SCADA', 'SHM System'] test_2 = ['CM', 'Finances'] test_3 = ['RBA', 'PBA'] df = pd.DataFrame({'a':[test_1,test_2,test_3],'b':[test_2]*3, 'c':[test_3]*3, 'product of len

我有一个数据框,它的值是列表。如何计算一行中所有列表长度的乘积,并存储在单独的列中?也许下面的例子可以说明这一点:

test_1 = ['Protocol', 'SCADA', 'SHM System'] 
test_2 = ['CM', 'Finances']
test_3 = ['RBA', 'PBA']

df = pd.DataFrame({'a':[test_1,test_2,test_3],'b':[test_2]*3, 'c':[test_3]*3, 'product of len(lists)':[12,8,8]})
这是一个示例代码,它显示在第一行中,乘积是3*2*2=12,这是第一行中每个列表的长度…对于其他行也是如此。 对于所有值都是列表的数据帧,如何计算这些产品并将其存储在新列中? 谢谢。

尝试使用并:

[外]


这对我有用。非常感谢。
df['product of len(lists)'] = df[['a', 'b', 'c']].applymap(len).product(axis=1)
                              a               b           c  product of len(lists)
0  [Protocol, SCADA, SHM System]  [CM, Finances]  [RBA, PBA]                     12
1                 [CM, Finances]  [CM, Finances]  [RBA, PBA]                      8
2                     [RBA, PBA]  [CM, Finances]  [RBA, PBA]                      8