Pandas数据帧创建问题

时间:2018-04-23 23:33:32

标签: python pandas dataframe

以下功能的目的是加载数据。 CITY_DATA [城市] 映射到用户定义的城市的相应.csv文件。

然而,当我" print(df)"时,我在所有阶段都得到一个空的数据帧。不知道这里发生了什么。

def load_data(city, month, day):
    """
    Loads data for the specified city and filters by month and day if applicable.

    Args:
        (str) city - name of the city to analyze
        (str) month - name of the month to filter by, or "all" to apply no month filter
        (str) day - name of the day of week to filter by, or "all" to apply no day filter
    Returns:
        df - Pandas DataFrame containing city data filtered by month and day
    """
    print (CITY_DATA[city])
    df = pd.read_csv(CITY_DATA[city])
    print (df)
    df['Start Time'] = pd.to_datetime(df['Start Time'])
    df['month'] = df['Start Time'].dt.month
    df['day_of_week'] = df['Start Time'].dt.weekday_name

    if month != 'all':
        months = ['january', 'february', 'march', 'april', 'may', 'june']
        month = months.index(month) + 1
        df = df[df['month'] == month]

    if day != 'all':
        df = df[df['day_of_week'] == day.title()]

    print (df)
    return df

任何帮助将不胜感激。

0 个答案:

没有答案