简体   繁体   English

在python中,如何制作一列中每个值与另一列中的值出现的次数(多少行)的矩阵?

[英]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.

相关问题 Pandas-如何获取另一列中每个对应值的行数 - Pandas- How to get number of times row occurs for each corresponding value in another column 如何从python矩阵的每一列中选择具有最多最小值的行? - How do I select a row with most number of minimum values out of each column from a matrix in python? 如何将数组的每个值复制一定次数(迭代行/列)? - How can I copy each value of an array for a certain number of times (Iteration over rows / column)? pandas 字符串在基于另一列的列中出现的次数 - pandas number of times a string occurs in one column based on another column 如何为在另一列 pandas 中具有相同值的那些行使一列的值相同 - How to make same value of one column for those rows which have same values in another column pandas 如何列出每个项目出现不同次数的列表? - How do I make a list where each item occurs a different number of times? Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 计算每个值在pandas列中出现的次数 - Count number of times each value occurs in pandas column 如何在另一个数据帧列pandas中检查一个数据帧的列值多少次? - how to check column value of one data frame how many times in another dataframe column pandas? 如何返回一列中的值与另一列中的另一个值不对齐的次数? - How do return the number of times a value in a column does not line up with another value in another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM