简体   繁体   中英

Iterate an operation considering multiple columns in Pandas DataFrame

I have a DataFrame link as Pic1:

To get Desired output i need to use below criteria Criteria:if Distance >85kms in at least 2 Routes from Cities column as base then return Ok; else Nok

Needed output to be saved in Shtout workbook

输入 期望的输出

import pandas as pd
df = pd.read_excel(path, sheet_name="Map")

import openpyxl
from openpyxl import Workbook
wb = openpyxl.load_workbook(path2)
wb.create_sheet("output")
ShtOut = wb["output"]

You could groupby cities then check the sum of distances over 85

df.groupby('Cities')['Distance'].apply(lambda x: 'Ok' if sum(x>85) >1 else 'Nok').reset_index(name='Result')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM