Python数据帧条件和

2024-05-19 22:47:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个国家,地区和收入的数据框架。我试图使用聚合返回平均值、最小值、最大值和计数。我希望能够计算收入大于100的国家。在

raw_data = {'Country': ['A', 'B', 'C', 'D', 'E'],
            'Region': ['X', 'X', 'X', 'Y', 'Y'],
            'Income': [100, 200, 300, 100, 200]
           }
incomeData = pd.DataFrame(raw_data, columns = ['Country', 'Region', 'Income'])
regionGroup = incomeData.groupby(['Region'], as_index=False)
groupCount = lambda x: x.count()
#CountHighIncome = ?
aggregations = {
    'Country': {groupCount
    },
    'Income': {'min', 'max', 'mean', 'median' #, CountHighIncome
    }
}
incomeSummary = regionGroup.agg(aggregations)
incomeSummary
^{pr2}$

请让我知道,lambda方法计算一个地区内的国家是否可以扩展到一个地区内收入大于100的国家。或者有更好的方法来解决这个问题。在

提前致谢。在


Tags: 方法lambdadataraw国家countryregion地区
1条回答
网友
1楼 · 发布于 2024-05-19 22:47:41

您可以使用带有sum条件的lambda的自定义函数,True的计数与{}相似,对于Country被删除{}函数且仅使用count

CountHighIncome = lambda x: (x > 100).sum()
aggregations = {
    'Country': {'count'
    },
    'Income': {'min', 'max', 'mean', 'median',  CountHighIncome
    }
}
incomeSummary = regionGroup.agg(aggregations)
print (incomeSummary)
  Region Income                           Country
            max  min <lambda> mean median   count
0      X    300  100        2  200    200       3
1      Y    200  100        1  150    150       2

相关问题 更多 >