0

In this program, the user may choose between three csv files. This is the gist of one of those three (let's call this as test_sample.csv):

CODE     AGE GROUP     SEX     CITY      
----     ---------     ---     ----      
E101      25 to 29      M      Denver     
E102      25 to 29      F      Denver     
E103      45 to 49      M      Chicago    
E104      45 to 49      F      Chicago     
E105      25 to 29      M      Denver     

What I wish to happen is suppose the user inputs a city, the program will show the information about that place. For example, I input "Denver," the program will show:

CODE     AGE GROUP     SEX     CITY      
----     ---------     ---     ----      
E101      25 to 29      M      Denver     
E102      25 to 29      F      Denver       
E105      25 to 29      M      Denver   

So far, I have only coded the first part (where the user chooses between the csv files). I have been stuck with the second part (showing specific information based on the city the user inputs), and I'm not sure what to do.

This is my code so far:

import pandas as pd

fileInput = str(input("Which file would you like to open: "))
fileOpen = open(fileInput)
df = pd.read_csv(fileOpen)

selectCity = input("Which city would you like to be visualized: ")

What should I do and where should I start?

thepajama
  • 55
  • 1
  • 9
  • 1
    This might answer your question https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values – Ricco D Dec 30 '20 at 06:19
  • Why you're using `open` ? and by default `input` takes value as `str` so no need to convert it into `str` – Sociopath Dec 30 '20 at 06:20
  • I could see you have not accepted answers for any of your question. If you find any of the answers solved your problem, you should mark them as resolved (_or upvote if you have the privilege_.). Hope you aware of how this site works. Check [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Arun Palanisamy Dec 30 '20 at 06:33
  • @thepajama the accepted answer doesn't work for your input csv, check the one I have supplied. – Jarvis Dec 30 '20 at 06:43
  • How does your csv files look like? Is it `comma separated` or `tab` as shown in your question? – Arun Palanisamy Dec 30 '20 at 06:54
  • @Jarvis I've tried the one you have supplied but the compiler said "KeyError: 'CITY'." I'm not exactly sure why, but the one I've accepted works. – thepajama Dec 30 '20 at 06:54
  • Your input csv contained whitespaces as delimiters, if your actual csv has commas, you should post that in your question. – Jarvis Dec 30 '20 at 06:55

2 Answers2

1

You can do this in multiple ways. One way is

import pandas as pd
fileInput = input("Which file would you like to open: ")
df = pd.read_csv(fileInput)

selectCity = input("Which city would you like to be visualized: ")
df2 = df[df['CITY']==selectCity]
print(df2)
Arun Palanisamy
  • 5,281
  • 6
  • 28
  • 53
  • OP had mentioned that he/she is using `csv` files(comma separated). I guess just for sample data, he/she would have given in the table format. Even in some of the questions, OP had used the same format for sample data @Jarvis – Arun Palanisamy Dec 30 '20 at 06:46
  • Have you read the question? They have clearly mentioned their input csv format and the desired/required output. – Jarvis Dec 30 '20 at 06:47
  • I did couple of times. @Jarvis. It was mentioned that `I have only coded the first part (where the user chooses between the csv files). ` and my understanding is that it is already working and he/she is stuck in second part which means reading csv part is fine with the sample code that is shown – Arun Palanisamy Dec 30 '20 at 06:51
  • Alright. Since my code is working I guess OP is using comma @Jarvis and my understanding is correct :) – Arun Palanisamy Dec 30 '20 at 06:56
  • 1
    Glad to know, you can ask the OP to accept your answer :) – Jarvis Dec 30 '20 at 06:56
1

First of all, the way you reading your input csv in pandas is wrong, you need to read it in your dataframe properly to separate columns:

import pandas as pd

fileInput = input("Which file would you like to open: ") # python3 input is str by default
df = pd.read_csv(fileInput, skiprows=[1], delimiter="\s{2,}", engine="python")

After that, you can easily extract rows for a particular city like this:

selectCity = input("Which city would you like to be visualized: ")
output = df[df["CITY"] == selectCity]

gives

  CODE AGE GROUP SEX    CITY
0  E101  25 to 29   M  Denver
1  E102  25 to 29   F  Denver
4  E105  25 to 29   M  Denver

You can format as per your need once you have the satisfying rows.

Jarvis
  • 8,494
  • 3
  • 27
  • 58