如何匹配pandas数据帧中的列并打印特定的行值

2024-10-01 17:21:47 发布

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

Iam尝试在用户输入与行中一个值匹配的值后匹配列中的行值。然后产生一些统计数据。Ex a用户输入航班始发地州“il”城市“chicago”dest city“la”state“CA”。你正从芝加哥机场代码ORD飞往洛杉矶机场代码LAX。距离为2000英里,适宜时间为3.25小时。根据时间计算,最好的航空公司是美国航空公司(AA),您将从芝加哥国际机场代码ORD飞往洛杉矶机场代码LAX。距离是2000英里。使用AA航空公司最快飞行时间为2小时。在

FlightDate  DayOfWeek   UniqueCarrier   Origin  OriginCityName  OriginState Dest    DestCityName    DestState   Route   DepTime ArrTime Delayed TaxiOut TaxiIn  DelayLength SchedDuration   ActualDuration  AirTime Distance
12/1/2017   Friday  B6  ATL Atlanta  GA BOS Boston  MA  ATL<-->BOS  948 1214    0   23  3   -26 165 146 120 946
12/1/2017   Friday  B6  ATL Atlanta  GA BOS Boston  MA  ATL<-->BOS  1208    1436    0   11  7   -26 166 148 130 946


OriginState = input('enter origin state ')
OriginCity = input('enter origin city ')
for i,r in df.iterrows():
    if r['OriginState'] == OriginState and r['OriginCityName'] == OriginCity:
        originplace = r['Origin']
    else:
        pass
DestState = input('enter destination state ')
DestCity = input('enter destination city ')
for index,row in df.iterrows():
    if row['DestState'] == DestState and row['DestCityName'] == DestCity:
        DestPlace = row['Dest']
    else:
        pass
print('You are flying from',OriginState,'airport code',originplace,'to',DestCity,'airport code',DestPlace,'.')

Tags: 代码用户cityinput时间rowstateenter
1条回答
网友
1楼 · 发布于 2024-10-01 17:21:47

您可以使用:
df_custom= df[(df['OriginState'] == OriginState) & ( df['OriginCityName'] == OriginCity) & (df['DestState'] == DestState) & (df['DestCityName'] == DestCity)]

然后使用df_custom来获得最短持续时间的最佳飞行。在

相关问题 更多 >

    热门问题