列表切片包含数据帧和字符串

Slicing of List contain datafram and strings

R_1 = {
    'market': 'Boston',
    'summary': pd.DataFrame({
        "year": [2022, 2023],
        "customers": [400, 230],
        "count": [180, 1150]}),
    }
}

R_2 = {
    'market': 'New York',
    'summary': pd.DataFrame({
        "year": [2022, 2023],
        "customers": [410, 220],
        "count": np.array([185, 115])}),

    }
}

然后我使用了一个函数来附加这些结果

    def get_results_max(self, market: str, metric: str, year: int) -> dict

        """
    I need help to write this function
        """

然后下面调用append all,结果文件很多

get_max(market= 'New York', metric= "count", year: 2022)

我需要 2022 年纽约的最大 box_count 值以及其他值的字典

你可以试试:

    ...

    def get_results_max(self, market, metric, year):
        metrics = [
            result["summary_yearly"].loc[
                result["summary_yearly"].year.eq(year), metric
            ].max()
            for result in self.results if result["market"] == market
        ]
        if metrics:
            return max(metrics)
        print(f"No result for market '{market}', metric '{metric}' and year '{year}'")

结果

sensi = Sensi()
sensi.add_result(RESULT_1)
sensi.add_result(RESULT_2)
print(sensi.get_results_max(market= 'New York', metric= "box_count", year=2022))

A new result for Boston has been added.
A new result for New York has been added.
188.70000000000002