【问题标题】:Find descriptor in csv file then give it a value在 csv 文件中查找描述符,然后给它一个值
【发布时间】:2020-11-11 03:32:51
【问题描述】:

这里很新,就像昨天开始的一样。我正在研究一种从这个 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 Print 'Green' 等 任何帮助表示赞赏

【问题讨论】:

标签: python pandas csv pandas-loc


【解决方案1】:

您的真正问题是不正确地使用loc,您没有指定要选择flight_category 列,因此您得到的结构既是station_id,又是flight_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

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多