简体   繁体   English

基于另一个 dataframe 中的两列的 dataframe 中的行条件计数

[英]Conditional count of rows in a dataframe based on two columns in another dataframe

I would like to add a column to df2 that includes a count of rows in df1 that have matching Herd and Ddat values.我想向df2添加一列,其中包括df1中具有匹配HerdDdat值的行数。

import pandas as pd 
    
df1 = [[52, '1', '1/1/2020'], [54, '1', '1/1/2020'],
       [55, '2', '1/1/2020'], [56, '3', '1/1/1999']]
    
df = pd.DataFrame(df1, columns =['Cow','Herd', 'Ddat'])

df2 = [['1', '1/1/2020'], ['1', '1/5/2020'],
       ['2', '1/1/2020'], ['3', '1/1/1999']]
    
df2 = pd.DataFrame(df2, columns =['Herd', 'Ddat'])

The output I am looking for is我正在寻找的 output 是

Herd    Ddat       Count
1     1/1/2020        2
1     1/5/2020        0
2     1/1/2020        1
3     1/1/1999        1

You can take advantage of the nice features of indexes:您可以利用索引的优点:

cols = ['Herd', 'Ddat']
new_df = df2.set_index(cols).assign(Count=df.groupby(cols).count()).fillna(0).astype({'Count': int}).reset_index()

Output: Output:

>>> new_df
  Herd      Ddat  Count
0    1  1/1/2020      2
1    1  1/5/2020      0
2    2  1/1/2020      1
3    3  1/1/1999      1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个 dataframe 中的条件表达式从 pandas DataFrame 中删除行 - Delete rows from a pandas DataFrame based on a conditional expression in another dataframe 如果 pyspark dataframe 基于两列中的值在另一个 dataframe 中,如何删除它们的行? - How to drop rows of a pyspark dataframe if they're in another dataframe based on the values from two columns? 根据两列比较和排列 dataframe 中的行? - Compare and rank rows in dataframe based on two columns? 基于两列合并行 dataframe - Python - Merge rows dataframe based on two columns - Python python:基于 pandas dataframe 中两列(变量)中的两个数组行的频率计数 - python: frequency count based on two array-rows in two columns (variables) in pandas dataframe 根据另一个 dataframe 对 dataframe 的列和行进行排序 - Sort both columns and rows of a dataframe based on another dataframe DataFrame 中的新列基于来自另一个 DataFrame 的行和列 - New column in DataFrame based on rows and columns from another DataFrame 如何根据另一个数据框上的列对数据框的行进行分类? - How to categories rows of a dataframe based on columns on another dataframe? Python/Pandas:基于另一个 dataframe 过滤和组织 dataframe 的行和列 - Python/Pandas: filter and organize the rows and columns of a dataframe based on another dataframe 比较一个数据框中的两列的行(如果存在) 蟒蛇 - Compare the rows of two columns in one dataframe if they exist another dataframe | python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM