Python Forum
Sorting data by specific variables using argparse
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting data by specific variables using argparse
#1
I have a data set with several columns. One column is consists of different years. I'm trying to create a program so that a user can input a year and the program filters the data and outputs information only from that year. I was told argparse should be able to make it easier, but I haven't been able to figure it out. Can anyone help with this? Thank you!!
Reply
#2
Could you add an example of the starting data and how you would like the resulting data to look?
Could you also show the code you have tried so far?
See the links in my signature for help on how and what to post to improve your question.
Reply
#3
Thanks for replying - I'm really new at this.
sample data:

Name Team Year Event Medal
Christian Mayer Austria 1998 Alpine Skiing Men's Combined Bronze
Mario Reiter Austria 1998 Alpine Skiing Men's Combined Gold
Josef Polig Italy 1992 Alpine Skiing Men's Combined Gold
Harald Christian Strand Nilsen Norway 1994 Alpine Skiing Men's Combined Bronze


what I've done so far - here, I'm also trying to filter by year and/or country, and then I get the error; I'm certain I'm missing something obvious.

import argparse
from argparse import ArgumentParser

parser = argparse.ArgumentParser()
parser.add_argument('-f')
parser.add_argument('-y', action='store_true')
parser.add_argument('-c', action='store_true')
args = parser.parse_args()


# Select include year and country from arguments
selection = input('Please input year and/or country:')
usecols = selection([])
if args.y: usecols.append('Year')
if args.c: usecols.append('Country')

# Read the file in the f argument with the specified columns.
year_country = december_project(args.f, usecols=usecols)
Reply
#4
this might work too, but it doesn't give an output:

import argparse
f = open(csvinput)

for line in f:
    if "Medal" not in line:
        data = line.split(",")
        name = data[0]
        event = data[1]
        year = data[2]
        country = data[3]
        print("Name: " + name + "\tEvent: " + event + "\tYear: " + year + "\tCountry: " + country)

print("Please provide a year:")
year = input()
print("Please provide a country:")
country = input()

for line in f:
    data = line.split(",")
    if data[2] == year and data[3] == country:
        name = data[0]
        event = data[1]
        year = data[2]
        country = data[3]
        print("Name: " + name + "\tEvent: " + event + "\tYear: " + year + "\tCountry: " + country)
Reply
#5
That should work. You don't need argparse. The only thing here is that you read all the lines of the file "csvinput" in your program between lines 4 and 11. So when the program reaches line 18, there is nothing left to read. Solution: close the file after line 11 and re-open it before line 18.
Reply
#6
I've been playing around with it, and I got it to work for the most part, except there's a problem. I can filter data by EITHER country or year, but not both. If I hash out one variable, it works, but I can't figure out how to combine it:

country = []
while True:
    value = str(input('To filter data, enter a country, type "done" when done: '))
    if value == 'done' :
        break
    try:
        value = str(value)
    except:
        print('Invalid input:')
        continue
    try:
        country.append(str(value))
    except:
        country.append(value)

print('you have entered: ',country)
print(type(country))

year = []
while True:
    value = input('Enter a year, type "done" when done: ')
    if value == 'done' :
        break
    try:
        value = int(value)
    except:
        print('Invalid input:')
        continue
    try:
        year.append(int(value))
    except:
        year.append(str(value))
print('you have entered:\n',year)


# filter out rows without year & country
olympics = name_country_year_event[name_country_year_event['Team'].isin(country) | name_country_year_event['Year'].isin(year)]

# print csv
print(olympics)
Any ideas?
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data Sorting and filtering(From an Excel File) PY_ALM 0 1,057 Jan-09-2023, 08:14 PM
Last Post: PY_ALM
  Training a model to identify specific SMS types and extract relevant data? lord_of_cinder 0 984 Oct-10-2022, 04:35 AM
Last Post: lord_of_cinder
Question Sorting data with pandas TheZaind 4 2,363 Nov-22-2021, 07:33 PM
Last Post: aserian
  Searching a .txt file for a specific number and extracting the corresponding data nrozman 3 3,241 Jul-27-2018, 02:07 PM
Last Post: nrozman
  How to filter specific rows from large data file Ariane 7 8,235 Jun-29-2018, 02:43 PM
Last Post: gontajones
  Plotting climate data with NetCdf files for a specific region with coordinates fyec 3 5,418 Jun-27-2018, 12:34 PM
Last Post: buran
  A small data sorting program - couple of general and hopefully easy questions Ansifatcat 2 2,939 Jan-25-2018, 05:29 AM
Last Post: Ansifatcat
  Replacing values for specific columns in Panda data structure Padowan 1 14,687 Nov-27-2017, 08:21 PM
Last Post: Padowan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020