Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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 如何计算常见的1';熊猫数据框中有什么?_Python_Pandas - Fatal编程技术网

Python 如何计算常见的1';熊猫数据框中有什么?

Python 如何计算常见的1';熊猫数据框中有什么?,python,pandas,Python,Pandas,我有以下数据,例如: A. B C 类别标签 0 1. 1. B_C 1. 1. 1. A_B_C 0 0 1. C 用于通过添加新列,其中列名称用于标签,计数1为sum,但仅由以下人员选择数字列: 如果列classifiedlabel存在,则最简单的是仅使用sum: df["countones"] = df.sum(axis=1) print (df) A B C D classifiedlabel countones 0 0 1 0 1

我有以下数据,例如:

A. B C 类别标签 0 1. 1. B_C 1. 1. 1. A_B_C 0 0 1. C 用于通过添加新列,其中列名称用于标签,计数
1
sum
,但仅由以下人员选择数字列:

如果列
classifiedlabel
存在,则最简单的是仅使用
sum

df["countones"] = df.sum(axis=1)
print (df)
   A  B  C  D classifiedlabel  countones
0  0  1  0  1             B_D          2
1  1  1  0  1           A_B_D          3
2  0  0  1  0               C          1
3  0  1  1  0             B_C          2
用于通过添加新列,其中列名称用于标签,计数
1
sum
,但仅由以下人员选择数字列:

如果列
classifiedlabel
存在,则最简单的是仅使用
sum

df["countones"] = df.sum(axis=1)
print (df)
   A  B  C  D classifiedlabel  countones
0  0  1  0  1             B_D          2
1  1  1  0  1           A_B_D          3
2  0  0  1  0               C          1
3  0  1  1  0             B_C          2
尝试:

尝试:


如果值为1/0,则可以使用:

(
    df.assign(
        count=df._get_numeric_data().sum(axis=1)
    )
)
输出:

   A  B  C  D classifiedlabel  count
0  0  1  0  1             B_D      2
1  1  1  0  1           A_B_D      3
2  0  0  1  0               C      1
3  0  1  1  0             B_C      2

如果值为1/0,则可以使用:

(
    df.assign(
        count=df._get_numeric_data().sum(axis=1)
    )
)
输出:

   A  B  C  D classifiedlabel  count
0  0  1  0  1             B_D      2
1  1  1  0  1           A_B_D      3
2  0  0  1  0               C      1
3  0  1  1  0             B_C      2

所以在输入数据中没有
Class\u标签
列,对吗?可以添加输入和预期输出吗?所以输入数据中没有
Class\u标签
列,对吗?是否可以添加输入和预期输出?类型错误:无法将序列与类型为'str'的非int相乘,我是面向类型error@srinivasmuralidharan-在我的解决方案之前是否有
classifiedlabel
?我遇到了问题resolved@srinivasmuralidharan-我的解决方案添加两列,只需要一列?它需要两列。TypeError:不能将序列与'str'类型的非int相乘,我是面向类型error@srinivasmuralidharan-在我的解决方案之前是否有
classifiedlabel
?我遇到了问题resolved@srinivasmuralidharan-我的解决方案添加两列,只需要一列?它需要两列。