使用用户输入过滤DataFrame

时间:2018-04-03 13:36:45

标签: python datetime variables dataframe user-input

我正在尝试使用用户输入来过滤DataFrame中的数据。 我是python和编程的新手 我研究了这个主题,但很难将我发现的东西应用到我的项目中。

$(function changelang() {
    $(".language a").click(function(e) {
        var selectedLanguage = $(this).attr("data-language");

        var currentContent = location.pathname.substring(
            location.pathname.indexOf("/", 1) + 1,
            location.pathname.length
        );

        // console.log(selectedLanguage, " >> ", currentContent);
        window.location.href = "/" + selectedLanguage + "/" + currentContent;
    });
});

目前我正在努力解决两件大事。

首先是如何使用月份&作为Excel文件的列过滤器的日期变量具有带有作为月份的数字的时间戳。我试图使用DateTime.month作为DateTime.weekday_name,但我不确定我是否已正确应用它或如何使用它来过滤列。

import time
import pandas as pd
import numpy as np
import datetime as dt

CITY_DATA = { 'chicago': 'chicago.csv',
              'new york city': 'new_york_city.csv',
              'washington': 'washington.csv' }

def get_filters():

    print('\n\nHello! Let\'s explore some US bikeshare data!')

    while True:
        city = input(str('\nWhich city would you like to see data on?'
'New York City, Chicago, or Washington?\n ').lower())
        if city in ('washington', 'chicago', 'new york city'):
            break
        elif city == 'new york':
            city += ' city'
            break
        else:
            print('\n\nYour answer does not match any of the above options, please try again!\n')


    months = ['january', 'february',
            'march', 'april',
            'may', 'june', 'all']

    while True:
        month = input(str('\nWould you like to search by one of the following months?\nJanuary, February, March, April, May, June, or all?\n ').lower())
        if month in months:
            break
        else:
            print ('Your answer does not match any of the above options, please try again!\n')

    days = ['all', 'monday', 'tuesday',
    'wednesday, thursday, friday',
    'saturday', 'sunday']

    while True:
        day = input(str('\nWould you like to search by one of the following days?\nSunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or all of them?\n' ).lower())
        if day in days:
            break
        else:
            print ('Your answer does not match any of the above options, please try again!\n')
    return city, month, day
    print('-'*40)

我正在努力的第二件事是如何结合所有用户输入并产生正确的结果。

def load_data(city, month, day):
    df = pd.read_csv(CITY_DATA[city])
    print ("printing {}...".format(city))
    df['Start Time'] = pd.to_datetime(df['Start Time'])
    df['Month'] = df['Month'].dt.month
    print('example month: {}'.format(df['Month']))
    df['Day of Week'] = df['Day of Week'].dt.weekday_name
    if month != 'all':
        df = df[df['Month'] == month[0]]
    if day != 'all':
        df = df[df['Day of Week'] == day[0]]       
    return df

0 个答案:

没有答案