pandas 名称错误:名称'flights_per_airport'未定义[已关闭]

olmpazwi  于 2023-05-12  发布在  其他
关注(0)|答案(1)|浏览(205)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

2小时前关闭
Improve this question
大家好,我在Juypter中有以下代码,但我一直得到NameError:name 'flights_per_airport' is not defined;我不知道我在这里做错了什么。
首先,我有def flights_per_airport:但它不起作用是不是打错了?

def flights_per_airport(flights_df, airports_df):
    
flights_per_airport_df = flights_df.merge(airports_df, left_on='ORIGIN_AIRPORT', right_on='IATA_CODE', how='left')
flights_per_airport_df = flights_per_airport_df.groupby(['IATA_CODE'], as_index=False)['ORIGIN_AIRPORT'].count()
flights_per_airport_df.rename(columns={'ORIGIN_AIRPORT': 'NUM_FLIGHTS'}, inplace=True)

return flights_per_airport_df
ao218c7q

ao218c7q1#

您的代码运行良好(具有正确的缩进),因此错误不在此处。您应该提供完整的Traceback,而不仅仅是最后一行:

def flights_per_airport(flights_df, airports_df):
    
    flights_per_airport_df = flights_df.merge(airports_df, left_on='ORIGIN_AIRPORT', right_on='IATA_CODE', how='left')
    flights_per_airport_df = flights_per_airport_df.groupby(['IATA_CODE'], as_index=False)['ORIGIN_AIRPORT'].count()
    flights_per_airport_df.rename(columns={'ORIGIN_AIRPORT': 'NUM_FLIGHTS'}, inplace=True)

    return flights_per_airport_df

# From Kaggle: https://www.kaggle.com/datasets/tylerx/flights-and-airports-data
df1 = pd.read_csv('flights.csv.zip').rename(columns={'OriginAirportID': 'ORIGIN_AIRPORT'})
df2 = pd.read_csv('airports.xls').rename(columns={'airport_id': 'IATA_CODE'})
out = flights_per_airport(df1, df2)

输出:

>>> out
    IATA_CODE  NUM_FLIGHTS
0       10140        17542
1       10299         7700
2       10397       148563
3       10423        26982
4       10529        13042
..        ...          ...
65      14893        24930
66      14908        23581
67      15016        33061
68      15304        35149
69      15376        10990

[70 rows x 2 columns]

相关问题