[英]Delete rows from a pandas DataFrame based on a conditional expression in another dataframe
[英]Conditional count of rows in a dataframe based on two columns in another dataframe
我想向df2
添加一列,其中包括df1
中具有匹配Herd
和Ddat
值的行數。
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'])
我正在尋找的 output 是
Herd Ddat Count
1 1/1/2020 2
1 1/5/2020 0
2 1/1/2020 1
3 1/1/1999 1
您可以利用索引的優點:
cols = ['Herd', 'Ddat']
new_df = df2.set_index(cols).assign(Count=df.groupby(cols).count()).fillna(0).astype({'Count': int}).reset_index()
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.