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.


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

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.


In [2]:
df  # lets look at the data


Out[2]:
Ac Ag Al Am Ar As At Au B Ba ... Tm Ts U V W Xe Y Yb Zn Zr
0 89 47 13 95 18 33 85 79 5 56 ... 69 117 92 23 74 54 39 70 30 40
1 Ac Ag Al Am Ar As At Au B Ba ... Tm Ts U V W Xe Y Yb Zn Zr
2 Actinium Silver Aluminium Americium Argon Arsenic Astatine Gold Boron Barium ... Thulium Tennessine Uranium Vanadium Tungsten Xenon Yttrium Ytterbium Zinc Zirconium
3 227 107.868 26.9815 243 39.9481 74.9216 210 196.967 10.81 137.328 ... 168.934 294 238.029 50.9415 183.841 131.294 88.9058 173.045 65.382 91.2242
4 actinide transition metal post-transition metal actinide noble gas metalloid metalloid transition metal metalloid alkaline earth metal ... lanthanide unknown,probably metalloid actinide transition metal transition metal noble gas transition metal lanthanide transition metal transition metal
5 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 ... 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392 1493462392
6 KTU KTU KTU KTU KTU KTU KTU KTU KTU KTU ... KTU KTU KTU KTU KTU KTU KTU KTU KTU KTU

7 rows × 118 columns


In [3]:
newdf = df.transpose()  # lets reshape the data some

In [4]:
newdf


Out[4]:
0 1 2 3 4 5 6
Ac 89 Ac Actinium 227 actinide 1493462392 KTU
Ag 47 Ag Silver 107.868 transition metal 1493462392 KTU
Al 13 Al Aluminium 26.9815 post-transition metal 1493462392 KTU
Am 95 Am Americium 243 actinide 1493462392 KTU
Ar 18 Ar Argon 39.9481 noble gas 1493462392 KTU
As 33 As Arsenic 74.9216 metalloid 1493462392 KTU
At 85 At Astatine 210 metalloid 1493462392 KTU
Au 79 Au Gold 196.967 transition metal 1493462392 KTU
B 5 B Boron 10.81 metalloid 1493462392 KTU
Ba 56 Ba Barium 137.328 alkaline earth metal 1493462392 KTU
Be 4 Be Beryllium 9.01218 alkaline earth metal 1493462392 KTU
Bh 107 Bh Bohrium 270 transition metal 1493462392 KTU
Bi 83 Bi Bismuth 208.98 post-transition metal 1493462392 KTU
Bk 97 Bk Berkelium 247 actinide 1493462392 KTU
Br 35 Br Bromine 79.904 diatomic nonmetal 1493462392 KTU
C 6 C Carbon 12.011 polyatomic nonmetal 1493462392 KTU
Ca 20 Ca Calcium 40.0784 alkaline earth metal 1493462392 KTU
Cd 48 Cd Cadmium 112.414 transition metal 1493462392 KTU
Ce 58 Ce Cerium 140.116 lanthanide 1493462392 KTU
Cf 98 Cf Californium 251 actinide 1493462392 KTU
Cl 17 Cl Chlorine 35.45 diatomic nonmetal 1493462392 KTU
Cm 96 Cm Curium 247 actinide 1493462392 KTU
Cn 112 Cn Copernicium 285 transition metal 1493462392 KTU
Co 27 Co Cobalt 58.9332 transition metal 1493462392 KTU
Cr 24 Cr Chromium 51.9962 transition metal 1493462392 KTU
Cs 55 Cs Cesium 132.905 alkali metal 1493462392 KTU
Cu 29 Cu Copper 63.5463 transition metal 1493462392 KTU
Db 105 Db Dubnium 268 transition metal 1493462392 KTU
Ds 110 Ds Darmstadtium 281 unknown, probably transition metal 1493462392 KTU
Dy 66 Dy Dysprosium 162.5 lanthanide 1493462392 KTU
... ... ... ... ... ... ... ...
Rg 111 Rg Roentgenium 282 unknown, probably transition metal 1493462392 KTU
Rh 45 Rh Rhodium 102.906 transition metal 1493462392 KTU
Rn 86 Rn Radon 222 noble gas 1493462392 KTU
Ru 44 Ru Ruthenium 101.072 transition metal 1493462392 KTU
S 16 S Sulfur 32.06 polyatomic nonmetal 1493462392 KTU
Sb 51 Sb Antimony 121.76 metalloid 1493462392 KTU
Sc 21 Sc Scandium 44.9559 transition metal 1493462392 KTU
Se 34 Se Selenium 78.9718 polyatomic nonmetal 1493462392 KTU
Sg 106 Sg Seaborgium 269 transition metal 1493462392 KTU
Si 14 Si Silicon 28.085 metalloid 1493462392 KTU
Sm 62 Sm Samarium 150.362 lanthanide 1493462392 KTU
Sn 50 Sn Tin 118.711 post-transition metal 1493462392 KTU
Sr 38 Sr Strontium 87.621 alkaline earth metal 1493462392 KTU
Ta 73 Ta Tantalum 180.948 transition metal 1493462392 KTU
Tb 65 Tb Terbium 158.925 lanthanide 1493462392 KTU
Tc 43 Tc Technetium 98 transition metal 1493462392 KTU
Te 52 Te Tellurium 127.603 metalloid 1493462392 KTU
Th 90 Th Thorium 232.038 actinide 1493462392 KTU
Ti 22 Ti Titanium 47.8671 transition metal 1493462392 KTU
Tl 81 Tl Thallium 204.38 post-transition metal 1493462392 KTU
Tm 69 Tm Thulium 168.934 lanthanide 1493462392 KTU
Ts 117 Ts Tennessine 294 unknown,probably metalloid 1493462392 KTU
U 92 U Uranium 238.029 actinide 1493462392 KTU
V 23 V Vanadium 50.9415 transition metal 1493462392 KTU
W 74 W Tungsten 183.841 transition metal 1493462392 KTU
Xe 54 Xe Xenon 131.294 noble gas 1493462392 KTU
Y 39 Y Yttrium 88.9058 transition metal 1493462392 KTU
Yb 70 Yb Ytterbium 173.045 lanthanide 1493462392 KTU
Zn 30 Zn Zinc 65.382 transition metal 1493462392 KTU
Zr 40 Zr Zirconium 91.2242 transition metal 1493462392 KTU

118 rows × 7 columns

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...


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

In [6]:
newdf  # now lets take another look


Out[6]:
Protons Symbol Name Mass Series Updated Initials
Ac 89 Ac Actinium 227 actinide 1493462392 KTU
Ag 47 Ag Silver 107.868 transition metal 1493462392 KTU
Al 13 Al Aluminium 26.9815 post-transition metal 1493462392 KTU
Am 95 Am Americium 243 actinide 1493462392 KTU
Ar 18 Ar Argon 39.9481 noble gas 1493462392 KTU
As 33 As Arsenic 74.9216 metalloid 1493462392 KTU
At 85 At Astatine 210 metalloid 1493462392 KTU
Au 79 Au Gold 196.967 transition metal 1493462392 KTU
B 5 B Boron 10.81 metalloid 1493462392 KTU
Ba 56 Ba Barium 137.328 alkaline earth metal 1493462392 KTU
Be 4 Be Beryllium 9.01218 alkaline earth metal 1493462392 KTU
Bh 107 Bh Bohrium 270 transition metal 1493462392 KTU
Bi 83 Bi Bismuth 208.98 post-transition metal 1493462392 KTU
Bk 97 Bk Berkelium 247 actinide 1493462392 KTU
Br 35 Br Bromine 79.904 diatomic nonmetal 1493462392 KTU
C 6 C Carbon 12.011 polyatomic nonmetal 1493462392 KTU
Ca 20 Ca Calcium 40.0784 alkaline earth metal 1493462392 KTU
Cd 48 Cd Cadmium 112.414 transition metal 1493462392 KTU
Ce 58 Ce Cerium 140.116 lanthanide 1493462392 KTU
Cf 98 Cf Californium 251 actinide 1493462392 KTU
Cl 17 Cl Chlorine 35.45 diatomic nonmetal 1493462392 KTU
Cm 96 Cm Curium 247 actinide 1493462392 KTU
Cn 112 Cn Copernicium 285 transition metal 1493462392 KTU
Co 27 Co Cobalt 58.9332 transition metal 1493462392 KTU
Cr 24 Cr Chromium 51.9962 transition metal 1493462392 KTU
Cs 55 Cs Cesium 132.905 alkali metal 1493462392 KTU
Cu 29 Cu Copper 63.5463 transition metal 1493462392 KTU
Db 105 Db Dubnium 268 transition metal 1493462392 KTU
Ds 110 Ds Darmstadtium 281 unknown, probably transition metal 1493462392 KTU
Dy 66 Dy Dysprosium 162.5 lanthanide 1493462392 KTU
... ... ... ... ... ... ... ...
Rg 111 Rg Roentgenium 282 unknown, probably transition metal 1493462392 KTU
Rh 45 Rh Rhodium 102.906 transition metal 1493462392 KTU
Rn 86 Rn Radon 222 noble gas 1493462392 KTU
Ru 44 Ru Ruthenium 101.072 transition metal 1493462392 KTU
S 16 S Sulfur 32.06 polyatomic nonmetal 1493462392 KTU
Sb 51 Sb Antimony 121.76 metalloid 1493462392 KTU
Sc 21 Sc Scandium 44.9559 transition metal 1493462392 KTU
Se 34 Se Selenium 78.9718 polyatomic nonmetal 1493462392 KTU
Sg 106 Sg Seaborgium 269 transition metal 1493462392 KTU
Si 14 Si Silicon 28.085 metalloid 1493462392 KTU
Sm 62 Sm Samarium 150.362 lanthanide 1493462392 KTU
Sn 50 Sn Tin 118.711 post-transition metal 1493462392 KTU
Sr 38 Sr Strontium 87.621 alkaline earth metal 1493462392 KTU
Ta 73 Ta Tantalum 180.948 transition metal 1493462392 KTU
Tb 65 Tb Terbium 158.925 lanthanide 1493462392 KTU
Tc 43 Tc Technetium 98 transition metal 1493462392 KTU
Te 52 Te Tellurium 127.603 metalloid 1493462392 KTU
Th 90 Th Thorium 232.038 actinide 1493462392 KTU
Ti 22 Ti Titanium 47.8671 transition metal 1493462392 KTU
Tl 81 Tl Thallium 204.38 post-transition metal 1493462392 KTU
Tm 69 Tm Thulium 168.934 lanthanide 1493462392 KTU
Ts 117 Ts Tennessine 294 unknown,probably metalloid 1493462392 KTU
U 92 U Uranium 238.029 actinide 1493462392 KTU
V 23 V Vanadium 50.9415 transition metal 1493462392 KTU
W 74 W Tungsten 183.841 transition metal 1493462392 KTU
Xe 54 Xe Xenon 131.294 noble gas 1493462392 KTU
Y 39 Y Yttrium 88.9058 transition metal 1493462392 KTU
Yb 70 Yb Ytterbium 173.045 lanthanide 1493462392 KTU
Zn 30 Zn Zinc 65.382 transition metal 1493462392 KTU
Zr 40 Zr Zirconium 91.2242 transition metal 1493462392 KTU

118 rows × 7 columns

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


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


Out[7]:
Protons Symbol Name
Ac 89 Ac Actinium
Ag 47 Ag Silver
Al 13 Al Aluminium
Am 95 Am Americium
Ar 18 Ar Argon
As 33 As Arsenic
At 85 At Astatine
Au 79 Au Gold
B 5 B Boron
Ba 56 Ba Barium

In [8]:
newdf.sort_values('Protons')


Out[8]:
Protons Symbol Name Mass Series Updated Initials
H 1 H Hydrogen 1.008 diatomic nonmetal 1498013115 KTU
He 2 He Helium 4.0026 noble gas 1493462392 KTU
Li 3 Li Lithium 6.94 alkali metal 1493462392 KTU
Be 4 Be Beryllium 9.01218 alkaline earth metal 1493462392 KTU
B 5 B Boron 10.81 metalloid 1493462392 KTU
C 6 C Carbon 12.011 polyatomic nonmetal 1493462392 KTU
N 7 N Nitrogen 14.007 diatomic nonmetal 1493462392 KTU
O 8 O Oxygen 15.999 diatomic nonmetal 1493462392 KTU
F 9 F Fluorine 18.9984 diatomic nonmetal 1493462392 KTU
Ne 10 Ne Neon 20.1798 noble gas 1493462392 KTU
Na 11 Na Sodium 22.9898 alkali metal 1493462392 KTU
Mg 12 Mg Magnesium 24.305 alkaline earth metal 1493462392 KTU
Al 13 Al Aluminium 26.9815 post-transition metal 1493462392 KTU
Si 14 Si Silicon 28.085 metalloid 1493462392 KTU
P 15 P Phosphorus 30.9738 polyatomic nonmetal 1493462392 KTU
S 16 S Sulfur 32.06 polyatomic nonmetal 1493462392 KTU
Cl 17 Cl Chlorine 35.45 diatomic nonmetal 1493462392 KTU
Ar 18 Ar Argon 39.9481 noble gas 1493462392 KTU
K 19 K Potassium 39.0983 alkali metal 1493462392 KTU
Ca 20 Ca Calcium 40.0784 alkaline earth metal 1493462392 KTU
Sc 21 Sc Scandium 44.9559 transition metal 1493462392 KTU
Ti 22 Ti Titanium 47.8671 transition metal 1493462392 KTU
V 23 V Vanadium 50.9415 transition metal 1493462392 KTU
Cr 24 Cr Chromium 51.9962 transition metal 1493462392 KTU
Mn 25 Mn Manganese 54.938 transition metal 1493462392 KTU
Fe 26 Fe Iron 55.8452 transition metal 1493462392 KTU
Co 27 Co Cobalt 58.9332 transition metal 1493462392 KTU
Ni 28 Ni Nickel 58.6934 transition metal 1493462392 KTU
Cu 29 Cu Copper 63.5463 transition metal 1493462392 KTU
Zn 30 Zn Zinc 65.382 transition metal 1493462392 KTU
... ... ... ... ... ... ... ...
Ac 89 Ac Actinium 227 actinide 1493462392 KTU
Th 90 Th Thorium 232.038 actinide 1493462392 KTU
Pa 91 Pa Protactinium 231.036 actinide 1493462392 KTU
U 92 U Uranium 238.029 actinide 1493462392 KTU
Np 93 Np Neptunium 237 actinide 1493462392 KTU
Pu 94 Pu Plutonium 244 actinide 1493462392 KTU
Am 95 Am Americium 243 actinide 1493462392 KTU
Cm 96 Cm Curium 247 actinide 1493462392 KTU
Bk 97 Bk Berkelium 247 actinide 1493462392 KTU
Cf 98 Cf Californium 251 actinide 1493462392 KTU
Es 99 Es Einsteinium 252 actinide 1493462392 KTU
Fm 100 Fm Fermium 257 actinide 1493462392 KTU
Md 101 Md Mendelevium 258 actinide 1493462392 KTU
No 102 No Nobelium 259 actinide 1493462392 KTU
Lr 103 Lr Lawrencium 266 actinide 1493462392 KTU
Rf 104 Rf Rutherfordium 267 transition metal 1493462392 KTU
Db 105 Db Dubnium 268 transition metal 1493462392 KTU
Sg 106 Sg Seaborgium 269 transition metal 1493462392 KTU
Bh 107 Bh Bohrium 270 transition metal 1493462392 KTU
Hs 108 Hs Hassium 269 transition metal 1493462392 KTU
Mt 109 Mt Meitnerium 278 unknown, probably transition metal 1493462392 KTU
Ds 110 Ds Darmstadtium 281 unknown, probably transition metal 1493462392 KTU
Rg 111 Rg Roentgenium 282 unknown, probably transition metal 1493462392 KTU
Cn 112 Cn Copernicium 285 transition metal 1493462392 KTU
Nh 113 Nh Nihonium 286 unknown, probably transition metal 1493462392 KTU
Fl 114 Fl Flerovium 289 post-transition metal 1493462392 KTU
Mc 115 Mc Moscovium 289 unknown, probably post transition metal 1493462392 KTU
Lv 116 Lv Livermorium 293 unknown, probably post transition metal 1493462392 KTU
Ts 117 Ts Tennessine 294 unknown,probably metalloid 1493462392 KTU
Og 118 Og Oganesson 294 unknown, predicted to be noble gas 1493462392 KTU

118 rows × 7 columns


In [9]:
type(newdf.Protons)


Out[9]:
pandas.core.series.Series

In [10]:
%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()


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).


In [16]:
newdf.Initials[1:5]


Out[16]:
Ag    KTU
Al    KTU
Am    KTU
Ar    KTU
Name: Initials, dtype: object

In [24]:
sorted = newdf.sort_values('Protons')
sorted


Out[24]:
Protons Symbol Name Mass Series Updated Initials
H 1 H Hydrogen 1.008 diatomic nonmetal 1498013115 KTU
He 2 He Helium 4.0026 noble gas 1493462392 KTU
Li 3 Li Lithium 6.94 alkali metal 1493462392 KTU
Be 4 Be Beryllium 9.01218 alkaline earth metal 1493462392 KTU
B 5 B Boron 10.81 metalloid 1493462392 KTU
C 6 C Carbon 12.011 polyatomic nonmetal 1493462392 KTU
N 7 N Nitrogen 14.007 diatomic nonmetal 1493462392 KTU
O 8 O Oxygen 15.999 diatomic nonmetal 1493462392 KTU
F 9 F Fluorine 18.9984 diatomic nonmetal 1493462392 KTU
Ne 10 Ne Neon 20.1798 noble gas 1493462392 KTU
Na 11 Na Sodium 22.9898 alkali metal 1493462392 KTU
Mg 12 Mg Magnesium 24.305 alkaline earth metal 1493462392 KTU
Al 13 Al Aluminium 26.9815 post-transition metal 1493462392 KTU
Si 14 Si Silicon 28.085 metalloid 1493462392 KTU
P 15 P Phosphorus 30.9738 polyatomic nonmetal 1493462392 KTU
S 16 S Sulfur 32.06 polyatomic nonmetal 1493462392 KTU
Cl 17 Cl Chlorine 35.45 diatomic nonmetal 1493462392 KTU
Ar 18 Ar Argon 39.9481 noble gas 1493462392 KTU
K 19 K Potassium 39.0983 alkali metal 1493462392 KTU
Ca 20 Ca Calcium 40.0784 alkaline earth metal 1493462392 KTU
Sc 21 Sc Scandium 44.9559 transition metal 1493462392 KTU
Ti 22 Ti Titanium 47.8671 transition metal 1493462392 KTU
V 23 V Vanadium 50.9415 transition metal 1493462392 KTU
Cr 24 Cr Chromium 51.9962 transition metal 1493462392 KTU
Mn 25 Mn Manganese 54.938 transition metal 1493462392 KTU
Fe 26 Fe Iron 55.8452 transition metal 1493462392 KTU
Co 27 Co Cobalt 58.9332 transition metal 1493462392 KTU
Ni 28 Ni Nickel 58.6934 transition metal 1493462392 KTU
Cu 29 Cu Copper 63.5463 transition metal 1493462392 KTU
Zn 30 Zn Zinc 65.382 transition metal 1493462392 KTU
... ... ... ... ... ... ... ...
Ac 89 Ac Actinium 227 actinide 1493462392 KTU
Th 90 Th Thorium 232.038 actinide 1493462392 KTU
Pa 91 Pa Protactinium 231.036 actinide 1493462392 KTU
U 92 U Uranium 238.029 actinide 1493462392 KTU
Np 93 Np Neptunium 237 actinide 1493462392 KTU
Pu 94 Pu Plutonium 244 actinide 1493462392 KTU
Am 95 Am Americium 243 actinide 1493462392 KTU
Cm 96 Cm Curium 247 actinide 1493462392 KTU
Bk 97 Bk Berkelium 247 actinide 1493462392 KTU
Cf 98 Cf Californium 251 actinide 1493462392 KTU
Es 99 Es Einsteinium 252 actinide 1493462392 KTU
Fm 100 Fm Fermium 257 actinide 1493462392 KTU
Md 101 Md Mendelevium 258 actinide 1493462392 KTU
No 102 No Nobelium 259 actinide 1493462392 KTU
Lr 103 Lr Lawrencium 266 actinide 1493462392 KTU
Rf 104 Rf Rutherfordium 267 transition metal 1493462392 KTU
Db 105 Db Dubnium 268 transition metal 1493462392 KTU
Sg 106 Sg Seaborgium 269 transition metal 1493462392 KTU
Bh 107 Bh Bohrium 270 transition metal 1493462392 KTU
Hs 108 Hs Hassium 269 transition metal 1493462392 KTU
Mt 109 Mt Meitnerium 278 unknown, probably transition metal 1493462392 KTU
Ds 110 Ds Darmstadtium 281 unknown, probably transition metal 1493462392 KTU
Rg 111 Rg Roentgenium 282 unknown, probably transition metal 1493462392 KTU
Cn 112 Cn Copernicium 285 transition metal 1493462392 KTU
Nh 113 Nh Nihonium 286 unknown, probably transition metal 1493462392 KTU
Fl 114 Fl Flerovium 289 post-transition metal 1493462392 KTU
Mc 115 Mc Moscovium 289 unknown, probably post transition metal 1493462392 KTU
Lv 116 Lv Livermorium 293 unknown, probably post transition metal 1493462392 KTU
Ts 117 Ts Tennessine 294 unknown,probably metalloid 1493462392 KTU
Og 118 Og Oganesson 294 unknown, predicted to be noble gas 1493462392 KTU

118 rows × 7 columns


In [26]:
sorted[sorted["Protons"] > 22].head()


Out[26]:
Protons Symbol Name Mass Series Updated Initials
V 23 V Vanadium 50.9415 transition metal 1493462392 KTU
Cr 24 Cr Chromium 51.9962 transition metal 1493462392 KTU
Mn 25 Mn Manganese 54.938 transition metal 1493462392 KTU
Fe 26 Fe Iron 55.8452 transition metal 1493462392 KTU
Co 27 Co Cobalt 58.9332 transition metal 1493462392 KTU

In [ ]: