[英]In python, how do I make a matrix of the number of times(how many rows) each value in one column occurs with values in another column?
I have a filtered data frame that changes how many rows it has, depending on how the user filters it.我有一个过滤的数据框,它根据用户过滤它的方式改变它有多少行。 I need to count how many times a value in one column matches with a value in another column for each row.
我需要为每一行计算一列中的值与另一列中的值匹配的次数。 for example: Lets say my DF is: sample data frame I would need a 3x3 matrix that has the sums of each time a value in column A occurs with a value in column B. The matrix would be: sample matrix I can determine what the dimension of the matrix should be, and what the unique values are for each column and how many times they occur, but need to find an efficient way to sum how frequently the values appear together.
例如:假设我的 DF 是:样本数据框我需要一个 3x3 矩阵,该矩阵具有 A 列中的值与 B 列中的值每次出现时的总和。矩阵将是:样本矩阵我可以确定是什么矩阵的维数应该是什么,每列的唯一值是什么以及它们出现的次数,但需要找到一种有效的方法来总结这些值一起出现的频率。
I was able to achieve your desired output like this:我能够像这样实现您想要的输出:
from collections import Counter
import pandas as pd
df = pd.DataFrame({'A':[500,500,300,400,400,300],'B':[10,10,20,10,20,30]})
inp = df.groupby('A')['B'].agg(Counter).to_frame()
out = pd.DataFrame.from_records(inp['B'].tolist(), index=inp.index).fillna(0).T
output:输出:
A 300 400 500
20 1.0 1.0 0.0
30 1.0 0.0 0.0
10 0.0 1.0 2.0
the groupyby/Counter counts how many occurrences of values in B occur in each value in A..then the second step unpacks the column of dictionaries into a dataframe groupyby/Counter 计算 A 中每个值中 B 中值出现的次数..然后第二步将字典列解压到数据帧中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.