import pandas
url = "http://thekirbster.pythonanywhere.com/api/elements?elem=all"
df = pandas.read_json(url)

"""
Explanation: Playing with Pandas
Pandas is able to read both .csv files and .json files.  Once the data is on-board in a pandas.Dataframe object, you may want to reshape or transform it in some way.  In the example below, Dataframe.transpose() is used.
End of explanation
"""


df  # lets look at the data

newdf = df.transpose()  # lets reshape the data some

newdf

"""
Explanation: The website above is all open source.  If you dig into the code at Github, you'll see how the HTTP request gets routed, by Flask, to the appropriate handler which, in this case, selects all rows from the Periodic_Table database, Elements table, and returns them in JSON format.
Pandas has taken that JSON data and turned it into a Dataframe object.
End of explanation
"""


newdf.rename(columns={0: 'Protons', 1: 'Symbol', 2: "Name", 3:'Mass', 
                      4:'Series', 5:'Updated', 6:'Initials'}, 
             inplace=True)

newdf  # now lets take another look

"""
Explanation: The raw JSON data did not include column names, so the default numeric indexing is used.  We have the ability to change those column names. Lets do it "in place" without creating a new Dataframe...
End of explanation
"""


newdf.iloc[0:10, 0:3]  # you may need to do some data cleaning as well

newdf.sort_values('Protons')

type(newdf.Protons)

%matplotlib inline
from matplotlib import pyplot as plt
x_axis = newdf.Protons
y_axis = newdf.Mass
plt.scatter(x_axis, y_axis)
plt.title('Protons versus Atomic Mass')
plt.xlabel('Protons')
plt.ylabel('Mass')
plt.show()

"""
Explanation: Now is your chance to practice retrieving "slices" of the data using df.loc or df.iloc.
End of explanation
"""


newdf.Initials[1:5]

sorted = newdf.sort_values('Protons')
sorted

sorted[sorted["Protons"] > 22].head()

"""
Explanation: That's right, atoms get heavier the more protons they have.  They have at least as many neutrons as protons, usually, and sometimes more. Remember some elements have isotopes, meaning differing numbers of neutrons for the same atomic number (number of protons, or positive charges in the nucleus).
End of explanation
"""


