es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Cortando la Lista que contiene un dataframe y cadenas de caracteres.

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

R_2 = {
‘market’: ‘Nueva York’,
‘summary’: pd.DataFrame({
“year”: [2022, 2023],
“customers”: [410, 220],
“count”: np.array([185, 115])
})
}

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

"""
Necesito ayuda para escribir esta función
"""

results = []

for r in [R_1, R_2]:
    if r['market'] == market:
        for index, row in r['summary'].iterrows():
            if row['year'] == year:
                results.append({'market': r['market'], 'year': year, 'customers': row['customers'], 'count': row[metric]})

max_count = max([result['count'] for result in results])

for result in results:
    if result['count'] == max_count:
        result['max_count'] = True
    else:
        result['max_count'] = False

return {'results': results}

Calling the function

getresultsmax(market=’Nueva York’, metric=’count’, year=2022)

Tags:  , , , ,

Answer

Comments are closed.