0

So I am attempting to plot the number of Shark Attacks by country from a supplied Dataset in the form of a CSV file. I am relatively new to Python and the Pandas and Matplotlib libraries and am having difficulty with syntax I believe.

Currently I'm running into an error:

AttributeError: 'NoneType' object has no attribute 'update'

Here is my Code:

import pandas as pd
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

#read in csv file
df = pd.read_csv('SharkAttack.csv',delimiter=",", encoding='cp1252', header=0)

#Correct Missing Country Names
df.loc[df['Area'].str.contains('Florida', na=False), 'Country'] = 'USA'

#correct Missing Values
df.fillna("NaN")



x = df['Country'].unique().tolist()


y = df['Country'].nunique()


plt.bar(x, y, align='center', alpha=0.5)

plt.ylabel('Number Of Attacks')
plt.xlabel("Country")
plt.title('Shark Attacks by Country')

plt.show()

Any idea how I can fix this? IT was simple enough to create an array when the number of unique items in the list was small enough to hard code, but there is a massive list of countries in the file.

taras
  • 5,216
  • 9
  • 32
  • 41

2 Answers2

0

You can make a plot with df itself:

df['Country'].value_counts().plot(kind='bar')
taras
  • 5,216
  • 9
  • 32
  • 41
  • I appreciate the comment, however I actually need to use MatPlotLib to make 3 different plots, a Line, Bar, and Pie Chart, so my issue is with the error message itself I suppose. – Mark Crabtree Jun 15 '18 at 22:29
  • Well, you can make all 3 kinds with pandas (which internally calls appropriate matplotlib functions) – taras Jun 15 '18 at 22:32
  • I see, So I attempted the use of your above code, however it just runs the file, I don't see a plot – Mark Crabtree Jun 15 '18 at 22:44
  • @MarkCrabtree: are you using the Jupyter notebook? If yes, please use [matplotlib magic command](https://stackoverflow.com/a/24884342/2204131). `%matplotlib inline`. Else above code should work – ramesh Jun 16 '18 at 00:13
0

For me the above code works in notebook. I have used this file as input https://github.com/ibmw/Shark-Attack/blob/master/shark_attack.csv