简体   繁体   English

计算具有特定条件的不同 Pandas 数据帧的值

[英]Counting Values from Different Pandas Dataframes with Certain Condition

I have datasets similar to this:我有类似于这样的数据集:

df1

company公司 date日期 act_call act_call act_visit行动访问 po
A一种 2022-10-01 2022-10-01 Yes是的 No No
B 2022-10-01 2022-10-01 Yes是的 No Yes是的
C C 2022-10-01 2022-10-01 No No No
B 2022-10-02 2022-10-02 No Yes是的 No
A一种 2022-10-02 2022-10-02 No Yes是的 No

df2

company公司 date日期 act_call act_call act_visit行动访问 po
D 2022-11-01 2022-11-01 Yes是的 No No
B 2022-11-01 2022-11-01 Yes是的 No Yes是的
C C 2022-11-01 2022-11-01 Yes是的 Yes是的 No
D 2022-11-02 2022-11-02 No Yes是的 No
A一种 2022-11-02 2022-11-02 No Yes是的 Yes是的

I want to count the number of company where the po is 'No' in df1 but also exists in df2 .我想计算podf1中为“否”但在df2中也存在的公司数量。

I tried using this code:我尝试使用此代码:

int_df = len(set(df2['company']).intersection(df1['po'].eq('no').groupby(df1['company'])))

but it returns below error:但它返回以下错误:

unhashable type: 'Series'

My expected output:我预期的 output:

2, (A, C) 2、(甲、丙)

*notes: the (A, C) doesn't have to be printed since I actually only want the number of the company. *注意:不必打印 (A, C),因为我实际上只想要公司的编号。

What would be the best code to my expected output?什么是我预期的 output 的最佳代码? Thank u in advance!提前谢谢你!

I would filter first the companies based on df2 with isin , then aggregate with groupy.all to identify the company with only "No", and sum :我会首先使用groupy.all过滤基于df2的公司,然后与isin聚合以识别只有“否”的公司,然后sum

(df1.loc[df1['company'].isin(df2['company']), 'po']
    .eq('No')
    .groupby(df1['company']).all()
    .sum()
)

Output: 2 Output: 2

暂无
暂无

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

相关问题 基于特定条件比较和计算来自 2 个(或更多)不同的 Pandas 数据帧的值 - Comparing and Count Values from 2 (or More) Different Pandas Dataframes Based on Certain Conditions 如果某些条件匹配,则将来自不同数据帧的两个值相加 python - Summing two values from different dataframes if certain criteria is matched python Pandas - ApplyMap 使用来自两个不同 DataFrame 的相应单元格值 - Pandas - ApplyMap using corresponding cell values from two different DataFrames 使用不同的DataFrame更改熊猫DataFrame切片的值 - Altering values from a pandas DataFrame slice using different DataFrames 从 2 个不同的数据帧熊猫添加两列的值 - add values of two columns from 2 different dataframes pandas 根据Pandas数据帧的其他列中的值计算列值 - Counting column values based on values in other columns for Pandas dataframes 如何在 pandas 中的 2 个不同数据帧的 3 列中找到匹配值,并在条件为真时执行操作 - How to find matching values in 3 columns of 2 different dataframes in pandas and perform an action when the condition is true Python 如何加入/合并 Pandas 数据帧与来自不同数据帧的特定值的匹配列 - Python how to join/merge Pandas dataframes with matching columns of specific values from different dataframes 计算满足熊猫数据框中某些求和条件的行数 - Counting the number of rows that meet certain sum condition in pandas dataframe 在两个不同的DataFrames Pandas中匹配字符串值 - Match string values in two different DataFrames Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM