python
]
Python
By Steve on July 29, 2019
Data Visualizations in Python and Jupyter Notebook Link to NB Viewer
import pandas as pd
from matplotlib import pyplot as plt
x = [1, 2, 3]
y = [1, 4, 9]
z = [10, 5, 0]
plt.plot(x, y)
plt.plot(x, z)
plt.title("Test Plot")
plt.xlabel("x")
plt.ylabel("y and z")
plt.legend(["This is Y", "This is Z"])
plt.show()
sample_data = pd.read_csv('sample_data.csv')
sample_data
column_a | column_b | column_c | |
---|---|---|---|
0 | 1 | 1 | 10 |
1 | 2 | 4 | 8 |
2 | 3 | 9 | 6 |
3 | 4 | 16 | 4 |
4 | 5 | 25 | 2 |
type(sample_data)
pandas.core.frame.DataFrame
sample_data.column_c
0 10
1 8
2 6
3 4
4 2
Name: column_c, dtype: int64
type(sample_data.column_c)
pandas.core.series.Series
sample_data.column_c.iloc[1]
8
plt.plot(sample_data.column_a, sample_data.column_b, 'o')
plt.plot(sample_data.column_a, sample_data.column_c)
plt.show()
data = pd.read_csv('countries.csv')
data
country | year | population | |
---|---|---|---|
0 | Afghanistan | 1952 | 8425333 |
1 | Afghanistan | 1957 | 9240934 |
2 | Afghanistan | 1962 | 10267083 |
3 | Afghanistan | 1967 | 11537966 |
4 | Afghanistan | 1972 | 13079460 |
5 | Afghanistan | 1977 | 14880372 |
6 | Afghanistan | 1982 | 12881816 |
7 | Afghanistan | 1987 | 13867957 |
8 | Afghanistan | 1992 | 16317921 |
9 | Afghanistan | 1997 | 22227415 |
10 | Afghanistan | 2002 | 25268405 |
11 | Afghanistan | 2007 | 31889923 |
12 | Albania | 1952 | 1282697 |
13 | Albania | 1957 | 1476505 |
14 | Albania | 1962 | 1728137 |
15 | Albania | 1967 | 1984060 |
16 | Albania | 1972 | 2263554 |
17 | Albania | 1977 | 2509048 |
18 | Albania | 1982 | 2780097 |
19 | Albania | 1987 | 3075321 |
20 | Albania | 1992 | 3326498 |
21 | Albania | 1997 | 3428038 |
22 | Albania | 2002 | 3508512 |
23 | Albania | 2007 | 3600523 |
24 | Algeria | 1952 | 9279525 |
25 | Algeria | 1957 | 10270856 |
26 | Algeria | 1962 | 11000948 |
27 | Algeria | 1967 | 12760499 |
28 | Algeria | 1972 | 14760787 |
29 | Algeria | 1977 | 17152804 |
... | ... | ... | ... |
1674 | Yemen, Rep. | 1982 | 9657618 |
1675 | Yemen, Rep. | 1987 | 11219340 |
1676 | Yemen, Rep. | 1992 | 13367997 |
1677 | Yemen, Rep. | 1997 | 15826497 |
1678 | Yemen, Rep. | 2002 | 18701257 |
1679 | Yemen, Rep. | 2007 | 22211743 |
1680 | Zambia | 1952 | 2672000 |
1681 | Zambia | 1957 | 3016000 |
1682 | Zambia | 1962 | 3421000 |
1683 | Zambia | 1967 | 3900000 |
1684 | Zambia | 1972 | 4506497 |
1685 | Zambia | 1977 | 5216550 |
1686 | Zambia | 1982 | 6100407 |
1687 | Zambia | 1987 | 7272406 |
1688 | Zambia | 1992 | 8381163 |
1689 | Zambia | 1997 | 9417789 |
1690 | Zambia | 2002 | 10595811 |
1691 | Zambia | 2007 | 11746035 |
1692 | Zimbabwe | 1952 | 3080907 |
1693 | Zimbabwe | 1957 | 3646340 |
1694 | Zimbabwe | 1962 | 4277736 |
1695 | Zimbabwe | 1967 | 4995432 |
1696 | Zimbabwe | 1972 | 5861135 |
1697 | Zimbabwe | 1977 | 6642107 |
1698 | Zimbabwe | 1982 | 7636524 |
1699 | Zimbabwe | 1987 | 9216418 |
1700 | Zimbabwe | 1992 | 10704340 |
1701 | Zimbabwe | 1997 | 11404948 |
1702 | Zimbabwe | 2002 | 11926563 |
1703 | Zimbabwe | 2007 | 12311143 |
1704 rows × 3 columns
# Compare the population growth in the US and China
data[data.country == 'United States']
country | year | population | |
---|---|---|---|
1608 | United States | 1952 | 157553000 |
1609 | United States | 1957 | 171984000 |
1610 | United States | 1962 | 186538000 |
1611 | United States | 1967 | 198712000 |
1612 | United States | 1972 | 209896000 |
1613 | United States | 1977 | 220239000 |
1614 | United States | 1982 | 232187835 |
1615 | United States | 1987 | 242803533 |
1616 | United States | 1992 | 256894189 |
1617 | United States | 1997 | 272911760 |
1618 | United States | 2002 | 287675526 |
1619 | United States | 2007 | 301139947 |
us = data[data.country == 'United States']
china = data[data.country == 'China']
us
country | year | population | |
---|---|---|---|
1608 | United States | 1952 | 157553000 |
1609 | United States | 1957 | 171984000 |
1610 | United States | 1962 | 186538000 |
1611 | United States | 1967 | 198712000 |
1612 | United States | 1972 | 209896000 |
1613 | United States | 1977 | 220239000 |
1614 | United States | 1982 | 232187835 |
1615 | United States | 1987 | 242803533 |
1616 | United States | 1992 | 256894189 |
1617 | United States | 1997 | 272911760 |
1618 | United States | 2002 | 287675526 |
1619 | United States | 2007 | 301139947 |
china
country | year | population | |
---|---|---|---|
288 | China | 1952 | 556263527 |
289 | China | 1957 | 637408000 |
290 | China | 1962 | 665770000 |
291 | China | 1967 | 754550000 |
292 | China | 1972 | 862030000 |
293 | China | 1977 | 943455000 |
294 | China | 1982 | 1000281000 |
295 | China | 1987 | 1084035000 |
296 | China | 1992 | 1164970000 |
297 | China | 1997 | 1230075000 |
298 | China | 2002 | 1280400000 |
299 | China | 2007 | 1318683096 |
plt.plot(us.year, us.population / 10**6)
plt.plot(china.year, china.population / 10**6)
plt.legend(['United States', 'China'])
plt.xlabel('year')
plt.ylabel('population')
plt.show()
us.population
1608 157553000
1609 171984000
1610 186538000
1611 198712000
1612 209896000
1613 220239000
1614 232187835
1615 242803533
1616 256894189
1617 272911760
1618 287675526
1619 301139947
Name: population, dtype: int64
us.population / us.population.iloc[0] * 100
1608 100.000000
1609 109.159457
1610 118.396984
1611 126.123908
1612 133.222471
1613 139.787246
1614 147.371256
1615 154.109114
1616 163.052553
1617 173.219018
1618 182.589685
1619 191.135648
Name: population, dtype: float64
plt.plot(us.year, us.population / us.population.iloc[0] * 100)
plt.plot(china.year, china.population / china.population.iloc[0] * 100)
plt.legend(['United States', 'China'])
plt.xlabel('year')
plt.ylabel('population growth(first year = 100)')
plt.show()
plt.xkcd()
plt.plot(us.year, us.population / us.population.iloc[0] * 100)
plt.plot(china.year, china.population / china.population.iloc[0] * 100)
plt.legend(['United States', 'China'])
plt.xlabel('year')
plt.ylabel('population growth(first year = 100)')
plt.show()
df = pd.read_csv('pokemon_data.csv')
df_xlsx = pd.read_excel('pokemon_data')
# print(df.tail(3))
# Name Type 1 Type 2 HP Attack Defense Sp. Atk \
797 720 HoopaHoopa Confined Psychic Ghost 80 110 60 150
798 720 HoopaHoopa Unbound Psychic Dark 80 160 60 170
799 721 Volcanion Fire Water 80 110 120 130
Sp. Def Speed Generation Legendary
797 130 70 6 True
798 130 80 6 True
799 90 70 6 True
# Read Headers
# print(df.columns)
# Read each column
# print(df['Name'])
# Read Each Row
# print(df.head(4))
# print(df.iloc[5:14])
# df.loc[df['Type 1'] == "Fire"]
# df.loc[df['Type 1'] == "Grass"]
df.describe()
# | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | |
---|---|---|---|---|---|---|---|---|
count | 800.000000 | 800.000000 | 800.000000 | 800.000000 | 800.000000 | 800.000000 | 800.000000 | 800.00000 |
mean | 362.813750 | 69.258750 | 79.001250 | 73.842500 | 72.820000 | 71.902500 | 68.277500 | 3.32375 |
std | 208.343798 | 25.534669 | 32.457366 | 31.183501 | 32.722294 | 27.828916 | 29.060474 | 1.66129 |
min | 1.000000 | 1.000000 | 5.000000 | 5.000000 | 10.000000 | 20.000000 | 5.000000 | 1.00000 |
25% | 184.750000 | 50.000000 | 55.000000 | 50.000000 | 49.750000 | 50.000000 | 45.000000 | 2.00000 |
50% | 364.500000 | 65.000000 | 75.000000 | 70.000000 | 65.000000 | 70.000000 | 65.000000 | 3.00000 |
75% | 539.250000 | 80.000000 | 100.000000 | 90.000000 | 95.000000 | 90.000000 | 90.000000 | 5.00000 |
max | 721.000000 | 255.000000 | 190.000000 | 230.000000 | 194.000000 | 230.000000 | 180.000000 | 6.00000 |
df.sort_values('Name', ascending = False)
# | Name | Type 1 | Type 2 | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | Legendary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
794 | 718 | Zygarde50% Forme | Dragon | Ground | 108 | 100 | 121 | 81 | 95 | 95 | 6 | True |
695 | 634 | Zweilous | Dark | Dragon | 72 | 85 | 70 | 65 | 70 | 58 | 5 | False |
46 | 41 | Zubat | Poison | Flying | 40 | 45 | 35 | 30 | 40 | 55 | 1 | False |
631 | 570 | Zorua | Dark | NaN | 40 | 65 | 40 | 80 | 40 | 65 | 5 | False |
632 | 571 | Zoroark | Dark | NaN | 60 | 105 | 60 | 120 | 60 | 105 | 5 | False |
286 | 263 | Zigzagoon | Normal | NaN | 38 | 30 | 41 | 30 | 41 | 60 | 3 | False |
707 | 644 | Zekrom | Dragon | Electric | 100 | 150 | 120 | 120 | 100 | 90 | 5 | True |
582 | 523 | Zebstrika | Electric | NaN | 75 | 100 | 63 | 80 | 63 | 116 | 5 | False |
157 | 145 | Zapdos | Electric | Flying | 90 | 90 | 85 | 125 | 90 | 100 | 1 | True |
367 | 335 | Zangoose | Normal | NaN | 73 | 115 | 60 | 60 | 60 | 90 | 3 | False |
793 | 717 | Yveltal | Dark | Flying | 126 | 131 | 95 | 131 | 98 | 99 | 6 | True |
520 | 469 | Yanmega | Bug | Flying | 86 | 76 | 86 | 116 | 56 | 95 | 4 | False |
208 | 193 | Yanma | Bug | Flying | 65 | 65 | 45 | 75 | 45 | 95 | 2 | False |
623 | 562 | Yamask | Ghost | NaN | 38 | 30 | 85 | 55 | 65 | 30 | 5 | False |
792 | 716 | Xerneas | Fairy | NaN | 126 | 131 | 95 | 131 | 98 | 99 | 6 | True |
192 | 178 | Xatu | Psychic | Flying | 65 | 75 | 70 | 95 | 70 | 95 | 2 | False |
394 | 360 | Wynaut | Psychic | NaN | 95 | 23 | 48 | 23 | 48 | 23 | 3 | False |
288 | 265 | Wurmple | Bug | NaN | 45 | 45 | 35 | 20 | 30 | 20 | 3 | False |
460 | 413 | WormadamTrash Cloak | Bug | Steel | 60 | 69 | 95 | 69 | 95 | 36 | 4 | False |
459 | 413 | WormadamSandy Cloak | Bug | Ground | 60 | 79 | 105 | 59 | 85 | 36 | 4 | False |
458 | 413 | WormadamPlant Cloak | Bug | Grass | 60 | 59 | 85 | 79 | 105 | 36 | 4 | False |
209 | 194 | Wooper | Water | Ground | 55 | 45 | 45 | 25 | 25 | 15 | 2 | False |
586 | 527 | Woobat | Psychic | Flying | 55 | 45 | 43 | 55 | 43 | 72 | 5 | False |
217 | 202 | Wobbuffet | Psychic | NaN | 190 | 33 | 58 | 33 | 58 | 33 | 2 | False |
301 | 278 | Wingull | Water | Flying | 40 | 30 | 30 | 55 | 30 | 85 | 3 | False |
45 | 40 | Wigglytuff | Normal | Fairy | 140 | 70 | 45 | 85 | 50 | 45 | 1 | False |
317 | 293 | Whismur | Normal | NaN | 64 | 51 | 23 | 51 | 23 | 28 | 3 | False |
372 | 340 | Whiscash | Water | Ground | 110 | 78 | 73 | 76 | 71 | 60 | 3 | False |
604 | 544 | Whirlipede | Bug | Poison | 40 | 55 | 99 | 40 | 79 | 47 | 5 | False |
607 | 547 | Whimsicott | Grass | Fairy | 60 | 67 | 85 | 77 | 75 | 116 | 5 | False |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
182 | 168 | Ariados | Bug | Poison | 70 | 90 | 70 | 60 | 60 | 40 | 2 | False |
628 | 567 | Archeops | Rock | Flying | 75 | 140 | 65 | 112 | 65 | 110 | 5 | False |
627 | 566 | Archen | Rock | Flying | 55 | 112 | 45 | 74 | 45 | 70 | 5 | False |
552 | 493 | Arceus | Normal | NaN | 120 | 120 | 120 | 120 | 120 | 120 | 4 | True |
64 | 59 | Arcanine | Fire | NaN | 90 | 110 | 80 | 100 | 80 | 95 | 1 | False |
29 | 24 | Arbok | Poison | NaN | 60 | 85 | 69 | 65 | 79 | 80 | 1 | False |
379 | 347 | Anorith | Rock | Bug | 45 | 95 | 50 | 40 | 50 | 75 | 3 | False |
196 | 181 | AmpharosMega Ampharos | Electric | Dragon | 90 | 95 | 105 | 165 | 110 | 45 | 2 | False |
195 | 181 | Ampharos | Electric | NaN | 90 | 75 | 85 | 115 | 90 | 55 | 2 | False |
652 | 591 | Amoonguss | Grass | Poison | 114 | 85 | 70 | 85 | 80 | 30 | 5 | False |
471 | 424 | Ambipom | Normal | NaN | 75 | 100 | 66 | 60 | 66 | 115 | 4 | False |
768 | 698 | Amaura | Rock | Ice | 77 | 59 | 50 | 67 | 63 | 46 | 6 | False |
366 | 334 | AltariaMega Altaria | Dragon | Fairy | 75 | 110 | 110 | 110 | 105 | 80 | 3 | False |
365 | 334 | Altaria | Dragon | Flying | 75 | 70 | 90 | 70 | 105 | 80 | 3 | False |
655 | 594 | Alomomola | Water | NaN | 165 | 75 | 80 | 40 | 45 | 65 | 5 | False |
71 | 65 | AlakazamMega Alakazam | Psychic | NaN | 55 | 50 | 65 | 175 | 95 | 150 | 1 | False |
70 | 65 | Alakazam | Psychic | NaN | 55 | 50 | 45 | 135 | 95 | 120 | 1 | False |
205 | 190 | Aipom | Normal | NaN | 55 | 70 | 55 | 40 | 55 | 85 | 2 | False |
333 | 306 | AggronMega Aggron | Steel | NaN | 70 | 140 | 230 | 60 | 80 | 50 | 3 | False |
332 | 306 | Aggron | Steel | Rock | 70 | 110 | 180 | 60 | 60 | 50 | 3 | False |
154 | 142 | AerodactylMega Aerodactyl | Rock | Flying | 80 | 135 | 85 | 70 | 95 | 150 | 1 | False |
153 | 142 | Aerodactyl | Rock | Flying | 80 | 105 | 65 | 60 | 75 | 130 | 1 | False |
751 | 681 | AegislashShield Forme | Steel | Ghost | 60 | 50 | 150 | 50 | 150 | 60 | 6 | False |
750 | 681 | AegislashBlade Forme | Steel | Ghost | 60 | 150 | 50 | 150 | 50 | 60 | 6 | False |
678 | 617 | Accelgor | Bug | NaN | 80 | 70 | 40 | 100 | 60 | 145 | 5 | False |
393 | 359 | AbsolMega Absol | Dark | NaN | 65 | 150 | 60 | 115 | 60 | 115 | 3 | False |
392 | 359 | Absol | Dark | NaN | 65 | 130 | 60 | 75 | 60 | 75 | 3 | False |
68 | 63 | Abra | Psychic | NaN | 25 | 20 | 15 | 105 | 55 | 90 | 1 | False |
511 | 460 | AbomasnowMega Abomasnow | Grass | Ice | 90 | 132 | 105 | 132 | 105 | 30 | 4 | False |
510 | 460 | Abomasnow | Grass | Ice | 90 | 92 | 75 | 92 | 85 | 60 | 4 | False |
800 rows × 12 columns