在 csv 文件中查找描述符然后给它一个值

Find descriptor in csv file then give it a value

这里很新,就像昨天开始的一样。我正在研究一种从这个 csv 文件中提取四种天气类别中的一种的方法。 VFR、MVFR、IFR、LIFR 是 csv 中每一行应具有的类别。

import pandas as pd

ap1 = input('Airport 1: ')
ap2 = input('Airport 2: ')
cat = pd.read_csv('https://www.aviationweather.gov/adds/dataserver_current/current/metars.cache.csv', skiprows=5,
                    index_col='station_id', usecols=['station_id', 'flight_category'])


ap1_cat = cat.loc[ap1]
ap2_cat = cat.loc[ap2]

if ap1_cat == 'VFR':
    print('Green')
elif:
    ap1_cat == 'MVFR':
    print('Blue')
else:
    print('Fail') 

我基本上想说,如果flight_category是VFR打印'Green'等等 感谢任何帮助

你真正的问题是 loc 的使用不正确你没有指定你想要 select flight_category 列所以你得到的结构既是 station_idflight_category。我个人将分类添加到 Dataframe,然后使用 loc

获取它
cat = pd.read_csv('https://www.aviationweather.gov/adds/dataserver_current/current/metars.cache.csv', skiprows=5,
                    index_col='station_id', usecols=['station_id', 'flight_category'])
df = cat.reset_index().merge(pd.DataFrame([{"flight_category":"VFR","Color":"Green"},
                        {"flight_category":"MVFR","Color":"Blue"}]),
         on="flight_category").fillna("Fail").set_index("station_id")
ap1 = "PKWA"
ap2 = "MMTO"
print(f"{ap1}: {df.loc[ap1,'Color']} {ap2}: {df.loc[ap2,'Color']}")

输出

PKWA: Green MMTO: Blue