Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tried everything
#1
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.
Reply
#2

  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.
Reply
#3
I think the problem with #3 is that count should be 'count' (that is, a string instead of a variable name).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
No unfortunately still not working with those changes :(
Reply
#5
? 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.
Reply
#6
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
Reply
#7
Try changing
np.where(int(a) > 10)
to

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


Forum Jump:

User Panel Messages

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