[英]How to count occurrences of a set of values for a range of columns in a pandas dataframe?
I have a pandas dataframe that looks something like this: 我有一个熊猫数据框,看起来像这样:
The dataframe is populated with 4 distinct strings: '00', '01', '10',
and '11'
. 数据帧填充有4个不同的字符串:
'00', '01', '10',
和'11'
。 I'm hoping to count each occurrence of the values in each column, so that the data above would return a resulting dataframe that looks something like this: 我希望对每列中值的每次出现进行计数,以便上面的数据将返回一个看起来像这样的结果数据框:
A B C D E
00 2 1 3 0 3
01 2 2 0 2 1
10 0 0 1 2 0
11 1 2 1 1 1
The original dataframe can be created with this code: 可以使用以下代码创建原始数据框:
dft = pd.DataFrame({'A' : ['11', '01', '01', '00', '00'],
'B' : ['00', '01', '11', '01', '11'],
'C' : ['00', '00', '10', '00', '11'],
'D' : ['10', '01', '11', '10', '01'],
'E' : ['00', '01', '00', '11', '00'],})
dft
You can use value_counts
together with a dictionary comprehension to generate the values, and then use the data to create a DataFrame. 您可以将
value_counts
与字典理解一起使用来生成值,然后使用数据来创建DataFrame。
>>> pd.DataFrame({col: dft[col].value_counts() for col in dft}).fillna(0)
A B C D E
00 2 1 3 0 3
01 2 2 0 2 1
10 0 0 1 2 0
11 1 2 1 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.