Python Forum
Tried everything - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Tried everything (/thread-17297.html)



Tried everything - Confused - Apr-05-2019

Sorry for asking really simple questions. I've been trying for hours with no luck.

1. Print the number of unique stations

import numpy as np 
data=pd.read_csv('train_station_routes.csv')
station = np.array(data['STATION'])
def unique(station): 
    x = np.array(station) 
    print(np.unique(x)) 
When I ran the code nothing happened

2. Print the most frequent station. Print out the number of times it appears in the list

import numpy as np 
data=pd.read_csv('train_station_routes.csv')
station = np.array(data['STATION'])
print np.argmax(station)
Error:
File "<ipython-input-69-fe0f4deaa23c>", line 9 print np.argmax(station) ^ SyntaxError: invalid syntax
3.Write your code to construct a dictionary variable: ***station_popularity***
whose keys are unique station and values are the corresponding counts computed from the dataset.
Print the dictionary.

import csv
with open('train_station_routes.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['STATION'], row[count])
Error:
<ipython-input-25-1761f5ad8e1d> in <module> 9 reader = csv.DictReader(csvfile) 10 for row in reader: ---> 11 print(row['STATION'], row[count]) 12 13 NameError: name 'count' is not defined
4. Write your code to find and print out the list of stations
that appears more than ten times in the dataset.

import numpy as np 
data=pd.read_csv('train_station_routes.csv')
stations = np.array(data['STATION'])
if stations >10
print (stations)
Error:
if stations >10 ^ SyntaxError: invalid syntax
Any advice would be amazing as I feel I am getting nowhere.


RE: Tried everything - Yoriz - Apr-05-2019


  1. defined a function but not called it.
  2. print is missing brackets ()
  3. : missing from end of if line and following line needs indenting.



RE: Tried everything - ichabod801 - Apr-05-2019

I think the problem with #3 is that count should be 'count' (that is, a string instead of a variable name).


RE: Tried everything - Confused - Apr-05-2019

No unfortunately still not working with those changes :(


RE: Tried everything - Yoriz - Apr-05-2019

? Cannot help with
Quote:still doesn't work

Show the code you tried in code tags and explain what you expect to happen and what is actually happening and any errors received in error tags.

Also the contents of 'train_station_routes.csv' is unknown.


RE: Tried everything - Confused - Apr-05-2019

Question 4.
Error:
<ipython-input-112-6b2400329a21> in <module> import numpy as np 11 data=pd.read_csv('train_station_routes.csv') 12 a= np.array(data['STATION']) ---> 13 np.where(int(a) > 10) 14 print(a) 15 TypeError: only size-1 arrays can be converted to Python scalars
Thanks


RE: Tried everything - Yoriz - Apr-05-2019

Try changing
np.where(int(a) > 10)
to

np.where(np.int_(a) > 10)