Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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 如何将值_计数应用于数据帧的所有元素?_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何将值_计数应用于数据帧的所有元素?

Python 如何将值_计数应用于数据帧的所有元素?,python,pandas,dataframe,Python,Pandas,Dataframe,将值_计数应用于序列很容易: In [1]: import pandas as pd In [2]: a = pd.DataFrame([[2,3],[2,2],[3,2],[2,1]]) In [3]: a Out[3]: 0 1 0 2 3 1 2 2 2 3 2 3 2 1 In [4]: a[0].value_counts() Out[4]: 2 3 3 1 Name: 0, dtype: int64 我需要像这样的东西 In [5]: a

将值_计数应用于序列很容易:

In [1]: import pandas as pd

In [2]: a = pd.DataFrame([[2,3],[2,2],[3,2],[2,1]])

In [3]: a
Out[3]: 
   0  1
0  2  3
1  2  2
2  3  2
3  2  1

In [4]: a[0].value_counts()
Out[4]: 
2    3
3    1
Name: 0, dtype: int64
我需要像这样的东西

In [5]: a.value_counts()
Out[5]: 
2    5
3    2
1    1
dtype: int64
但是,
a.value\u counts()
返回
“DataFrame”对象没有属性“value\u counts”


如何将值\u计数应用于数据帧的元素?

您可以转换为numpy,将数据展平,然后再次将其更改为一个系列:

pd.Series(a.to_numpy().reshape(-1)).value_counts()

2    5
3    2
1    1
dtype: int64

可以转换为numpy,展平数据,然后再次将其更改为一个系列:

pd.Series(a.to_numpy().reshape(-1)).value_counts()

2    5
3    2
1    1
dtype: int64

a.apply(pd.value\u counts)
@Erfan,您的评论不是我问的,您将我的问题标记为询问不同内容的问题的副本。布鲁诺·梅洛给出了正确的答案。请你再读一遍我的问题和你标记为重复的问题。
a.apply(pd.value\u counts)
@Erfan,你的评论不是我所问的,你将我的问题标记为一个问了不同问题的问题的重复。布鲁诺·梅洛给出了正确的答案。请您再次阅读我的问题和您标记为重复的问题。谢谢@Bruno Mello,它工作得很好,尽管我必须通过
更改为
,可能是因为我使用的熊猫版本。是的,它只适用于>=0.24的版本。我终于在这里找到了一个巴西人:)谢谢你@Bruno Mello,它工作得很好,尽管我不得不通过
更改为_numpy()
,可能是因为我使用的熊猫版本。是的,它只适用于>=0.24的版本。我终于在这里找到了一个巴西人:)