简体   繁体   English

使用python(pandas)从多个csv文件中过滤列值

[英]Filter column value from multiple csv files using python(pandas)

CITY_DATA = { 'chicago': 'chicago.csv','new york city': 'new_york_city.csv','washington': 'washington.csv' } CITY_DATA = {'芝加哥':'chicago.csv','纽约市':'new_york_city.csv','华盛顿':'washington.csv'}

Asks user to specify a city, month, and day to analyze. 要求用户指定要分析的城市,月份和日期。

Returns:
    (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

To DO:get user input for city (chicago, new york city, washington). 要执行的操作:获取城市(芝加哥,纽约,华盛顿)的用户输入。 HINT: Use a while loop to handle invalid input 提示:使用while循环处理无效输入

Here is top 5 rows of chicago below which is one of 3 csv file. 这是芝加哥的前5行,其下是3个csv文件之一。 My question is there are total 3 csv files for each city. 我的问题是每个城市总共有3个csv文件。 How can I filter column value(city/month/day) from different files? 如何从不同文件中过滤列值(城市/月/日)? If using loop, it seems wrong that I use if, elif,elif.... for all cities or 12 months or 7 days. 如果使用循环,则在所有城市或12个月或7天使用elif,elif ....似乎是错误的。 Sorry, I am new to Python and I am dizzy for this. 抱歉,我是Python的新手,对此我感到头晕。 Please help with a answer or a hint. 请提供答案或提示。 Thanks. 谢谢。

            Start Time             End Time  Trip Duration  \
0  2017-05-29 18:36:27  2017-05-29 18:49:27            780   
1  2017-06-12 19:00:33  2017-06-12 19:24:22           1429   
2  2017-02-13 17:02:02  2017-02-13 17:20:10           1088   
3  2017-04-24 18:39:45  2017-04-24 18:54:59            914   
4  2017-01-26 15:36:07  2017-01-26 15:43:21            434   

                  Start Station                          End Station  \
0     Columbus Dr & Randolph St                 Federal St & Polk St   
1        Kingsbury St & Erie St  Orleans St & Merchandise Mart Plaza   
2         Canal St & Madison St              Paulina Ave & North Ave   
3  Spaulding Ave & Armitage Ave       California Ave & Milwaukee Ave   
4        Clark St & Randolph St         Financial Pl & Congress Pkwy   

    User Type  Gender  Birth Year  
0  Subscriber    Male      1991.0  
1    Customer     NaN         NaN  
2  Subscriber  Female      1982.0  
3  Subscriber    Male      1966.0  
4  Subscriber  Female      1983.0  

What is wrong with the code below? 以下代码有什么问题? Should place city=input('Enter a city') after if statement? 是否应该在if语句后放置city = input('Enter a city')? confused. 困惑。

import time
import pandas as pd
import numpy as np

CITY_DATA = { 'chicago': 'chicago.csv',
             'new york city': 'new_york_city.csv',
             'washington': 'washington.csv' }
def get_city():
    print("Hello! Let's explore some US bikeshare data! \n Which city would you like? \n Chicago, New York City or Washington? ")
cities = ['chicago', 'new york city', 'washington']
city = input('Enter a city: ')
Enter a city: san jose

if city == 'chicago':
    return chicago
elif city == 'new york city':
    return new_york_city
elif city == 'washington':
    return washington
else:
    print ('Ops, your enter is out of range.')

File "<ipython-input-14-335bd5bdf8dc>", line 2
    return chicago
    ^
SyntaxError: 'return' outside function

This is the data analysis project for Udacity. 这是Udacity的数据分析项目。 This is how I did it. 这就是我做的。

import time
import pandas as pd
import numpy as np

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

def get_filters():
    """
    Asks user to specify a city, month, and day to analyze.

    Returns:
        (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
    """
    print('Hello! Let\'s explore some US bikeshare data!')

    cities = ['chicago', 'new york city', 'washington']
    cond = 0
    while cond != 1:
        city = input("Which city would you like? ")
        city = city.lower()
        if city in cities:
            cond = 1
        else:
            cond = 0



        # TO DO: get user input for month (all, january, february, ... , june)
    months = ['all' , 'january', 'february', 'march','april','may','june','july','august','september''october','november','december']
    cond = 0
    while cond != 1:
        month = input("Which month would you like? ")
        month = month.lower()
        if month in months:
            cond = 1
        else:
            cond = 0



        # TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
    days = ['all', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday','sunday']
    cond = 0
    while cond != 1:
        day = input("Which day would you like? ")
        day = day.lower()
        if day in days:
            cond = 1
        else:
            cond = 0



    print('-'*40)
    return city, month, day

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用pandas PYTHON按列中的值合并两个CSV文件 - How to merge two CSV files by value in column using pandas PYTHON 使用 python 按列值移动多个 csv 文件 - move multiple csv files by column value using python 分组以计算多个 CSV 文件中唯一 ID/itemName 的出现次数,并使用 pandas/python 对另一列中的值求和 - Group by to count occurrences of unique ID/itemName across multiple CSV files and sum up value in another column using pandas/python 使用Python和Pandas合并多个CSV文件 - Combine multiple CSV files using Python and Pandas 使用 python pandas 加入多个 CSV 文件 - Join multiple CSV files by using python pandas 从现有的 csv 文件 python pandas 创建多个 csv 文件 - Creating multiple csv files from existing csv file python pandas 尝试使用 python 中的 pandas 过滤具有多个变量的 CSV 文件 - Trying to filter a CSV file with multiple variables using pandas in python Select 来自多个 csv 文件的特定列,然后使用 pandas 将这些列合并到单个文件中 - Select specific column from multiple csv files, then merge those columns into single file using pandas 如何使用 Python pandas 更快地读取多个 CSV 文件 - How to read faster multiple CSV files using Python pandas Python:将多个 csv 文件从目录读入字典,而不使用 Pandas - Python: Read in multiple csv files from a directory into a dictionary without using Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM