熊猫填充数据框,其值在另一个dataframe范围内的值计数

发布于 2025-02-09 02:05:33 字数 1380 浏览 2 评论 0原文

我目前有两个dataframes,df_ages和df_count:

In  [1]: df_ages
Out [1]: 
        Enrolled         Age
    1          Y          44    
    2          Y          35    
    3          N          37        
    4          Y          55    
    5          N          26    
    6          Y          19    
    7          N          18    
    8          N          49        
    9          Y          26    
    10         Y          25
    11         Y          25    
    12         Y          32    
    13         Y          25        
    14         N          50    
    15         N          58        

In  [2]: df_count
Out [2]: 
             Min         Max    counts     percentage
    1         18          25        
    2         26          35        
    3         36          45        
    4         46          55        
    5         56          65        

我正在寻找代码填充df_count [count]列的代码,其中包含在上一列中的最小年龄和最大年龄范围内的人的总和。

[百分比]列应为条目数量的百分比。

所需的结果输出如下所示:

In  [2]: df_count
Out [2]: 
             Min         Max    counts    percentage
    1         18          25         5          33.3
    2         26          35         4          26.7
    3         36          45         2          13.3
    4         46          55         3          20.0
    5         56          65         1           6.7

I currently have two dataframes, df_ages and df_count:

In  [1]: df_ages
Out [1]: 
        Enrolled         Age
    1          Y          44    
    2          Y          35    
    3          N          37        
    4          Y          55    
    5          N          26    
    6          Y          19    
    7          N          18    
    8          N          49        
    9          Y          26    
    10         Y          25
    11         Y          25    
    12         Y          32    
    13         Y          25        
    14         N          50    
    15         N          58        

In  [2]: df_count
Out [2]: 
             Min         Max    counts     percentage
    1         18          25        
    2         26          35        
    3         36          45        
    4         46          55        
    5         56          65        

I am looking for code to populate df_count [count] column with the sum of people who fit within the min and max age range in the previous columns.

The [percentage] column should be the percentage of number of entries.

The desired resulting output is shown below:

In  [2]: df_count
Out [2]: 
             Min         Max    counts    percentage
    1         18          25         5          33.3
    2         26          35         4          26.7
    3         36          45         2          13.3
    4         46          55         3          20.0
    5         56          65         1           6.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2025-02-16 02:05:33

您可以尝试使用使用在行上使用series.betweew

df_count['counts'] = df_count.apply(lambda row: df_ages['Age'].between(row['Min'], row['Max']).sum(), axis=1)
df_count['percentage'] = df_count['counts'].div(len(df_ages)).mul(100).round(1)
print(df_count)

   Min  Max  counts  percentage
0   18   25       5        33.3
1   26   35       4        26.7
2   36   45       2        13.3
3   46   55       3        20.0
4   56   65       1         6.7

You can try apply on rows with Series.between

df_count['counts'] = df_count.apply(lambda row: df_ages['Age'].between(row['Min'], row['Max']).sum(), axis=1)
df_count['percentage'] = df_count['counts'].div(len(df_ages)).mul(100).round(1)
print(df_count)

   Min  Max  counts  percentage
0   18   25       5        33.3
1   26   35       4        26.7
2   36   45       2        13.3
3   46   55       3        20.0
4   56   65       1         6.7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文