Lecture 20 exercise - section 2

Lecture 20 exercise - section 2#

Live-coded solution.

Electricity data#

import pandas as pd

electricity = pd.read_csv("data/P_Data_Extract_From_World_Development_Indicators/0a6eb0d7-9c99-4d18-bf17-7839ff3ad1ed_Data.csv")
electricity
Series Name Series Code Country Name Country Code 1990 [YR1990] 2000 [YR2000] 2014 [YR2014] 2015 [YR2015] 2016 [YR2016] 2017 [YR2017] 2018 [YR2018] 2019 [YR2019] 2020 [YR2020] 2021 [YR2021] 2022 [YR2022] 2023 [YR2023]
0 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG .. 4.4 89.5 71.5 97.7 97.7 93.4 97.7 97.7 97.7 85.3 ..
1 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 100 99.4 100 100 99.9 99.9 100 100 100 100 100 ..
2 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA .. 98.6 99.3 99.4 99.4 99.5 99.6 99.5 99.7 99.8 100 ..
3 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM .. .. .. .. .. .. .. .. .. .. .. ..
4 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 100 100 100 100 100 100 100 100 100 100 100 ..
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
266 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
267 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
268 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
269 Data from database: World Development Indicators NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
270 Last Updated: 10/24/2024 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

271 rows × 16 columns

pd.melt(electricity, id_vars=["Series Name", "Series Code", "Country Name", "Country Code"])
Series Name Series Code Country Name Country Code variable value
0 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 1990 [YR1990] ..
1 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 1990 [YR1990] 100
2 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 1990 [YR1990] ..
3 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 1990 [YR1990] ..
4 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 1990 [YR1990] 100
... ... ... ... ... ... ...
3247 NaN NaN NaN NaN 2023 [YR2023] NaN
3248 NaN NaN NaN NaN 2023 [YR2023] NaN
3249 NaN NaN NaN NaN 2023 [YR2023] NaN
3250 Data from database: World Development Indicators NaN NaN NaN 2023 [YR2023] NaN
3251 Last Updated: 10/24/2024 NaN NaN NaN 2023 [YR2023] NaN

3252 rows × 6 columns

electricity_by_year = pd.melt(
    electricity,
    id_vars=["Series Name", "Series Code", "Country Name", "Country Code"],
    var_name="Year",
    value_name="Access to elec.",
)
electricity_by_year
Series Name Series Code Country Name Country Code Year Access to elec.
0 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 1990 [YR1990] ..
1 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 1990 [YR1990] 100
2 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 1990 [YR1990] ..
3 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 1990 [YR1990] ..
4 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 1990 [YR1990] 100
... ... ... ... ... ... ...
3247 NaN NaN NaN NaN 2023 [YR2023] NaN
3248 NaN NaN NaN NaN 2023 [YR2023] NaN
3249 NaN NaN NaN NaN 2023 [YR2023] NaN
3250 Data from database: World Development Indicators NaN NaN NaN 2023 [YR2023] NaN
3251 Last Updated: 10/24/2024 NaN NaN NaN 2023 [YR2023] NaN

3252 rows × 6 columns

Plot#

import plotly.express as px

px.line(
    electricity_by_year,
    x="Year",
    y="Access to elec.",
    color="Country Name",
    title="Access to electricity for each country by year",
)
electricity_by_year.dtypes
Series Name        object
Series Code        object
Country Name       object
Country Code       object
Year               object
Access to elec.    object
dtype: object
electricity_by_year["Access to elec."].astype(float)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 electricity_by_year["Access to elec."].astype(float)

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/pandas/core/generic.py:6643, in NDFrame.astype(self, dtype, copy, errors)
   6637     results = [
   6638         ser.astype(dtype, copy=copy, errors=errors) for _, ser in self.items()
   6639     ]
   6641 else:
   6642     # else, only a single dtype is given
-> 6643     new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
   6644     res = self._constructor_from_mgr(new_data, axes=new_data.axes)
   6645     return res.__finalize__(self, method="astype")

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/pandas/core/internals/managers.py:430, in BaseBlockManager.astype(self, dtype, copy, errors)
    427 elif using_copy_on_write():
    428     copy = False
--> 430 return self.apply(
    431     "astype",
    432     dtype=dtype,
    433     copy=copy,
    434     errors=errors,
    435     using_cow=using_copy_on_write(),
    436 )

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/pandas/core/internals/managers.py:363, in BaseBlockManager.apply(self, f, align_keys, **kwargs)
    361         applied = b.apply(f, **kwargs)
    362     else:
--> 363         applied = getattr(b, f)(**kwargs)
    364     result_blocks = extend_blocks(applied, result_blocks)
    366 out = type(self).from_blocks(result_blocks, self.axes)

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/pandas/core/internals/blocks.py:758, in Block.astype(self, dtype, copy, errors, using_cow, squeeze)
    755         raise ValueError("Can not squeeze with more than one column.")
    756     values = values[0, :]  # type: ignore[call-overload]
--> 758 new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
    760 new_values = maybe_coerce_values(new_values)
    762 refs = None

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/pandas/core/dtypes/astype.py:237, in astype_array_safe(values, dtype, copy, errors)
    234     dtype = dtype.numpy_dtype
    236 try:
--> 237     new_values = astype_array(values, dtype, copy=copy)
    238 except (ValueError, TypeError):
    239     # e.g. _astype_nansafe can fail on object-dtype of strings
    240     #  trying to convert to float
    241     if errors == "ignore":

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/pandas/core/dtypes/astype.py:182, in astype_array(values, dtype, copy)
    179     values = values.astype(dtype, copy=copy)
    181 else:
--> 182     values = _astype_nansafe(values, dtype, copy=copy)
    184 # in pandas we don't store numpy str dtypes, so convert to object
    185 if isinstance(dtype, np.dtype) and issubclass(values.dtype.type, str):

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/pandas/core/dtypes/astype.py:133, in _astype_nansafe(arr, dtype, copy, skipna)
    129     raise ValueError(msg)
    131 if copy or arr.dtype == object or dtype == object:
    132     # Explicit copy, or required since NumPy can't view from / to object.
--> 133     return arr.astype(dtype, copy=True)
    135 return arr.astype(dtype, copy=copy)

ValueError: could not convert string to float: '..'
list(electricity_by_year["Access to elec."].unique())
['..',
 '100',
 '95.7630462646484',
 '92.1548004150391',
 '99.9413986206055',
 '99.9842529296875',
 '87.475116',
 '94.606559753418',
 '99.846923828125',
 '92.257427',
 '89.9',
 '99.8443069458008',
 '70.334586',
 '96.8',
 '99.8828430175781',
 '99.8492813110352',
 '99.0415649414063',
 '98.9990844726563',
 '27.3',
 '99.8532638549805',
 '96.6253509521484',
 '70.19',
 '99.9834289550781',
 '91.3068161010742',
 '32.8',
 '88.7',
 '13.9',
 '99.8425200685198',
 '99.546800179424',
 '99.9943911362469',
 nan,
 '4.4',
 '99.4',
 '98.6',
 '24.2',
 '97.7',
 '95.7',
 '98.9',
 '91.7',
 '32',
 '89',
 '79',
 '21.5',
 '31.2',
 '70',
 '99',
 '26.5',
 '94.4',
 '97.1',
 '88.1',
 '9.1',
 '2.5',
 '58.6',
 '16.6',
 '41',
 '6',
 '3.1',
 '97.9',
 '96.7',
 '95.2',
 '39.8',
 '6.7',
 '29.4',
 '96.9',
 '48.7',
 '97',
 '56',
 '81.1',
 '88.8',
 '93.7',
 '84.5',
 '64.8',
 '29.3',
 '20.4',
 '12.7',
 '76.8',
 '73.6',
 '34.3',
 '99.8',
 '43.7',
 '99.9',
 '86',
 '73.3',
 '15.2',
 '74.6',
 '33.7',
 '67.4',
 '60.3',
 '86.3',
 '84.4',
 '99.6',
 '55.6',
 '42.6',
 '99.3',
 '4.3',
 '99.5',
 '12.6',
 '4.8',
 '83.8',
 '9.6',
 '68.6',
 '19.1',
 '98',
 '46',
 '98.7',
 '67.3',
 '99.1',
 '69.8',
 '6.1',
 '41.9',
 '36.5',
 '29.9',
 '73',
 '6.5',
 '43.2',
 '72.8',
 '81.4',
 '12.4',
 '89.6',
 '72.5',
 '74.7',
 '97.8',
 '6.2',
 '88',
 '52.9',
 '37.7',
 '94.1',
 '7.7',
 '4.7',
 '2.1',
 '72.4',
 '70.3',
 '95.4',
 '80',
 '23',
 '93.4',
 '98.5',
 '8.7',
 '82.1',
 '17.8',
 '17',
 '85.6',
 '91.3',
 '94.8',
 '95.6',
 '94.6',
 '7.4',
 '97.6',
 '22.2',
 '88.2',
 '99.7',
 '49.2',
 '16.7',
 '19.9638817610371',
 '34.1432102135773',
 '81.0997416649668',
 '87.0272667097087',
 '97.9262635329594',
 '67.5690834435773',
 '92.080656312843',
 '91.1581940859182',
 '91.1583141767936',
 '99.9939024980174',
 '99.1137058587738',
 '98.9925837200275',
 '98.2465834253865',
 '99.4761938755859',
 '40.5982967572402',
 '17.8675254053848',
 '99.4524626648336',
 '84.5263170160897',
 '74.0831790968894',
 '55.9714119039902',
 '26.4350562984302',
 '36.4025530411247',
 '95.2456141733554',
 '91.722504391963',
 '91.113646802215',
 '91.532905512629',
 '20.1822161091548',
 '72.8544678231575',
 '15.8054672531362',
 '58.2676706695706',
 '92.4180531531316',
 '91.426218615127',
 '91.3401715052384',
 '77.299823288762',
 '99.6359280517518',
 '72.4961541796735',
 '55.4438771570273',
 '99.7152423608883',
 '24.871639267962',
 '72.7326037651714',
 '57.891737765281',
 '25.659147015754',
 '25.6507452124749',
 '94.3274648640233',
 '78.3543617623921',
 '89.5',
 '62.4',
 '91.5',
 '34.1',
 '91.4',
 '90',
 '60',
 '99.2',
 '95.9',
 '19.2',
 '7',
 '85.3',
 '56.1',
 '56.8',
 '12',
 '8.4',
 '71.8',
 '13.5',
 '43.4',
 '61.9',
 '58.9',
 '95.1',
 '66',
 '44.6',
 '65',
 '27.2',
 '93',
 '87.1',
 '53',
 '78.3',
 '91',
 '85.5',
 '33.5',
 '17.2',
 '86.9',
 '39.6',
 '85.1',
 '93.3',
 '36',
 '83.4',
 '37.8',
 '82.5',
 '27.8',
 '9.4',
 '75.5',
 '11.9',
 '34.2',
 '38.8',
 '71.2',
 '97.3',
 '24.8',
 '52',
 '48.4',
 '84.9',
 '81.9',
 '15.7',
 '54.2',
 '90.1',
 '90.8',
 '18.1',
 '92.9',
 '89.8',
 '98.4',
 '19.8',
 '61',
 '18.8',
 '50.5',
 '51.4',
 '85.9',
 '92.7',
 '98.1',
 '44.9',
 '90.2',
 '23.5',
 '45.7',
 '49.1',
 '66.1',
 '27.9',
 '32.3',
 '31.860474023504',
 '47.6625364866446',
 '88.8342958618003',
 '93.9555303410912',
 '99.4060407563452',
 '84.7781290421301',
 '96.650227815738',
 '96.2870699100877',
 '97.0185492289121',
 '99.8754093344162',
 '99.7837597994349',
 '99.7496943982014',
 '99.8614239812103',
 '50.0334416807631',
 '35.3989999768092',
 '99.9189168958054',
 '94.4349904993138',
 '84.0374385425314',
 '68.7595094033684',
 '44.9427767431572',
 '52.9422008030466',
 '99.0133006644742',
 '97.0593672921663',
 '96.8125407490586',
 '97.0063562569368',
 '39.4566989775184',
 '83.1677876328261',
 '32.2789179055918',
 '78.3455492706838',
 '96.5203825283056',
 '95.9487475084581',
 '95.9037641046041',
 '88.5572255305389',
 '99.8482663987896',
 '87.0248640437107',
 '76.6814711878895',
 '99.9313827467578',
 '39.8441097568589',
 '86.7803650873228',
 '83.8523954854089',
 '38.2658690780816',
 '38.2601223073109',
 '98.8894942576766',
 '86.3012690468959',
 '71.5',
 '42',
 '74',
 '97.4',
 '91.8',
 '29.6',
 '62.1',
 '96.6',
 '16.2',
 '7.9',
 '86.4',
 '62.7',
 '98.2',
 '74.1',
 '44.3',
 '62.6',
 '59.1',
 '98.8',
 '45.8',
 '64.1',
 '29',
 '93.2',
 '87.3',
 '54.7',
 '90.5',
 '34.9',
 '20.1',
 '40.8',
 '97.5',
 '94.9',
 '41.6',
 '90.6',
 '40',
 '89.7',
 '31.8',
 '73.8',
 '23.8',
 '10.8',
 '37.6',
 '92.1',
 '39.5',
 '24',
 '60.5',
 '51.6',
 '82',
 '83.2',
 '52.5',
 '91.6',
 '18.4',
 '93.9',
 '89.1',
 '95.5',
 '22.8',
 '19.7',
 '55.1',
 '51.2',
 '94.3',
 '48',
 '95',
 '26.2',
 '44.7',
 '18.5',
 '52.3',
 '31.1',
 '33.9038001286462',
 '46.7587390583886',
 '89.0475351878897',
 '94.4839855823945',
 '99.5333634023846',
 '86.8527487873062',
 '96.9635036230764',
 '96.6357464510822',
 '97.342378290966',
 '99.1669776184071',
 '99.7676812016782',
 '98.3274195169609',
 '99.8916168370621',
 '50.0818317262279',
 '35.4839102740872',
 '99.4422835628403',
 '95.1884141896381',
 '84.8979365612511',
 '69.0584544888191',
 '47.0891947927519',
 '54.457463855061',
 '98.7891607198295',
 '97.2889505262466',
 '97.0711664557134',
 '97.2370514445961',
 '42.2813802769193',
 '84.1863196499328',
 '32.2126229767553',
 '80.7234409574285',
 '96.5086550115235',
 '95.9328003127985',
 '95.8874892892129',
 '89.763281693845',
 '99.8396896396459',
 '87.8757714925346',
 '78.703229504003',
 '99.9440320084058',
 '40.0606293618327',
 '87.7309920878869',
 '86.7867132149237',
 '39.1121832624488',
 '39.1065437139548',
 '98.9709975774677',
 '87.0237851218521',
 '41.8',
 '75.9',
 '37',
 '64.2',
 '8.5',
 '87.5',
 '68.8',
 '59.8',
 '13.4',
 '9.2',
 '76.4',
 '17.3',
 '45.3',
 '64.3',
 '96',
 '66.2',
 '46.9',
 '63.4',
 '42.9',
 '56.4',
 '79.3',
 '22.9',
 '40.4',
 '53.1',
 '92.6',
 '42.1',
 '35.1',
 '17.7',
 '72.2',
 '11',
 '41.2',
 '74.8',
 '49.7',
 '90.7',
 '83.9',
 '59.3',
 '92.4',
 '94.2',
 '92',
 '69.4',
 '64.5',
 '20.3',
 '58.2',
 '51.1',
 '5.3',
 '95.8',
 '76.5',
 '46.8',
 '26.7',
 '57.8',
 '68.9',
 '35.4',
 '42.5',
 '38.8546244221511',
 '50.906114634764',
 '89.5411657691967',
 '95.1697890618449',
 '99.6409785046505',
 '88.3686079048487',
 '97.0422752719492',
 '96.7243162467113',
 '97.4042780321237',
 '99.8283065055591',
 '99.8655891836026',
 '99.655469757283',
 '99.9170013923783',
 '54.1947420802944',
 '40.6329285874508',
 '99.8758820464439',
 '95.847095252009',
 '86.2839085579144',
 '72.8985169734875',
 '50.9990853826559',
 '58.3264142215853',
 '99.1246661085904',
 '97.4818550592041',
 '97.3029797168612',
 '97.4319920019967',
 '46.3321398827951',
 '85.5482587344368',
 '37.4463085105182',
 '82.7714915723345',
 '96.7983436224903',
 '96.276014130497',
 '96.2332941143712',
 '90.7857334721579',
 '99.89585109751',
 '89.1589196955869',
 '80.1272307017537',
 '99.9571187558814',
 '44.61180063733',
 '88.9341020251841',
 '88.9173147181527',
 '43.7370505625674',
 '43.7319091609899',
 '99.0052638487001',
 '88.1922255251927',
 '34.5',
 '9.3',
 '88.6',
 '60.9',
 '14.1',
 '10.9',
 '78.7',
 '18',
 '46.2',
 '65.6',
 '60.2',
 '66.4',
 '73.5',
 '56.2',
 '92.2',
 '26',
 '43.3',
 '86.5',
 '55.8',
 '85.7',
 '44.2',
 '93.6',
 '70.6',
 '24.1',
 '34.8',
 '93.8',
 '24.3',
 '86.6',
 '84.6',
 '17.6',
 '54.4',
 '74.3',
 '61.7',
 '23.4',
 '62.9',
 '52.1',
 '4.2',
 '32.1',
 '79.5',
 '32.4',
 '62.8',
 '79.2',
 '40.3',
 '44',
 '40.199898068195',
 '48.789457473528',
 '90.6663018742983',
 '95.7430331185145',
 '99.86914260178',
 '90.5262379043035',
 '97.6980233866031',
 '97.4517427779889',
 '98.1128558182722',
 '99.9637094037616',
 '99.9183821940095',
 '99.9272265326044',
 '99.9698703678352',
 '54.5631752651004',
 '41.5183905621843',
 '99.9608075777366',
 '96.6140192932522',
 '87.2637509200286',
 '71.9060547127056',
 '54.4361536322367',
 '60.2679332649365',
 '99.2795749258714',
 '97.7135942305631',
 '97.5717475090532',
 '97.6667483687171',
 '50.2107357057394',
 '86.565149962679',
 '39.0805495764912',
 '84.7280368393812',
 '97.6250026473811',
 '97.2291081354939',
 '97.1983752877486',
 '91.815222539659',
 '99.9405764596022',
 '90.4945894126707',
 '82.3564554155498',
 '99.970583083607',
 '44.320455294993',
 '90.2630998844596',
 '91.6760241733433',
 '43.6808105316438',
 '43.675736407086',
 '43.6808105316439',
 '99.1339177479467',
 '89.0186672372511',
 '39',
 '92.8',
 '68.3',
 '14.4',
 '9.5',
 '80.7',
 '62.2',
 '14.6',
 '10.1',
 '81',
 '18.7',
 '47',
 '67.1',
 '66.5',
 '44.8',
 '80.4',
 '92.3',
 '45',
 '28.4',
 '90.9',
 '44.4',
 '61.2',
 '46.3',
 '67',
 '50.9',
 '95.3',
 '43.6',
 '85.2',
 '56.5',
 '38',
 '71',
 '26.1',
 '65.8',
 '50.7',
 '84.7',
 '6.3',
 '55.3',
 '50',
 '61.6',
 '62',
 '40.2',
 '45.4',
 '43.0171479832647',
 '51.2110549404709',
 '89.1692812379426',
 '95.804767395244',
 '99.8729516393806',
 '92.0349140485874',
 '97.6650202588857',
 '97.4161347769004',
 '98.0496113702534',
 '99.9704727607712',
 '99.942320081113',
 '99.940819336018',
 '99.9708561296605',
 '54.5831226685803',
 '44.0132817192788',
 '99.9631057051384',
 '97.7587635009644',
 '88.2960951797823',
 '73.3946899985265',
 '55.3160844961813',
 '61.3394550392999',
 '99.2872327792157',
 '97.7603349993766',
 '97.5791568486266',
 '97.7119543642317',
 '51.1674138439625',
 '87.6492919680925',
 '40.4843611286372',
 '86.9830368829659',
 '96.1467646904579',
 '95.5104255149385',
 '95.4583357981029',
 '92.9486959090124',
 '99.8985605663644',
 '91.5254892263974',
 '82.9591610078136',
 '99.981741114911',
 '47.0762627878568',
 '91.0865817462377',
 '94.6179244479771',
 '46.3373545996798',
 '46.3326008959214',
 '99.152882923018',
 '89.8908858380532',
 '45.6',
 '70.1',
 '9.8',
 '84',
 '63.2',
 '14.3',
 '83.3',
 '47.7',
 '64.7',
 '66.6',
 '50.3',
 '77',
 '96.1',
 '90.3',
 '83.5',
 '42.2',
 '30.9',
 '91.1',
 '69.7',
 '89.3',
 '23.1',
 '68.5',
 '31',
 '11.2',
 '47.9',
 '80.1',
 '29.7',
 '53.3',
 '55.4',
 '23.6',
 '46.6',
 '75.1',
 '70.4',
 '22.7',
 '85',
 '6.8',
 '57.6',
 '89.2',
 '41.3',
 '43',
 '46.7',
 '44.3812594428681',
 '51.1680826551065',
 '90.3581538695401',
 '96.2215700753372',
 '99.9455010885556',
 '92.834365738276',
 '97.8779427927934',
 '97.6524316332846',
 '98.2621398882088',
 '99.9533142481858',
 '99.9663713381538',
 '99.9065322711927',
 '99.9875256636779',
 '55.5016312834383',
 '45.0522458898094',
 '99.9505379687558',
 '97.8867643059813',
 '88.6615853993001',
 '74.1134591811169',
 '57.0959550724622',
 '62.7544694323566',
 '99.3356536729549',
 '98.0015266427987',
 '97.8532110437475',
 '97.9559417265741',
 '52.9514648159524',
 '88.0490889564092',
 '41.836982681727',
 '87.6841442253686',
 '97.1324839563419',
 '96.6517404120541',
 '96.6125775683893',
 '93.3328680629391',
 '99.9400367116293',
 '92.7013041706379',
 '84.5931602439996',
 '99.9929550285056',
 '47.5782396795133',
 '92.2015549079081',
 '95.3232055481217',
 '47.1303990695105',
 '47.1257966456512',
 '99.2510521973761',
 '90.199100032655',
 '96.2',
 '97.2',
 '15.4',
 '69.9',
 '66.7',
 '85.4',
 '33.4',
 '93.1',
 '96.5',
 '47.5',
 '27.6',
 '11.5',
 '50.6',
 '98.3',
 '30.6',
 '94.5',
 '20.5',
 '96.4',
 '45.2',
 '26.3',
 '72.9',
 '49.9',
 '7.3',
 '59.7',
 '39.9',
 '54.1',
 '73.9',
 '52.7',
 '46.2648745577187',
 '51.7308990685072',
 '90.6384870529171',
 '97.0414890291467',
 '99.9796417892388',
 '93.4926075631317',
 '97.8013659805564',
 '97.5687165889393',
 '98.1511649991063',
 '99.9760236823625',
 '99.9777773685799',
 '99.9520178888209',
 '99.9953534951789',
 '56.346408400069',
 '46.3640414093557',
 '99.9686544956864',
 '98.0667929887089',
 '89.0003692130539',
 '74.6422206071065',
 '58.6069711033586',
 '63.9296623394879',
 '99.3926777630817',
 '98.152759448219',
 '98.0060017525721',
 '98.1092788328358',
 '54.562507330301',
 '88.4120504553677',
 '43.217947106415',
 '88.4522403939564',
 '97.2315412836112',
 '96.7733482603745',
 '96.7352691074454',
 '93.673300524618',
 '99.9332673681265',
 '93.6071297305702',
 '86.4225510641771',
 '99.9971273501603',
 '48.3855447902348',
 '93.2090070960632',
 '96.197039231936',
 '48.478195544667',
 '48.4737889046716',
 '99.1899480241711',
 '90.4854035957629',
 '48.2',
 '73.7',
 '19',
 '10.2',
 '65.4',
 '11.3',
 '87.9',
 '20.8',
 '71.1',
 '66.8',
 '82.9',
 '63.7',
 '35.8',
 '47.2',
 '52.6',
 '50.4',
 '29.8',
 '70.2',
 '14.2',
 '53.4',
 '83.6',
 '31.5',
 '55.2',
 '18.6',
 '59.5',
 '20.9',
 '78.5',
 '68',
 '27.5',
 '76.3',
 '49.3',
 '61.8',
 '42.7',
 '55.7',
 '74.9',
 '49',
 '48.100862134193',
 '54.224724307904',
 '90.8493820461977',
 '97.3939355970029',
 '99.9864361919036',
 '95.2851631707137',
 '98.1555450173615',
 '97.9617970162443',
 '98.5234655577502',
 '99.9881579022417',
 '99.9616380974146',
 '99.9763188018388',
 '99.9969239356276',
 '57.8615299350207',
 '48.0509504079887',
 '99.9798902530907',
 '99.1169517100785',
 '90.1059784759654',
 '76.6323166518695',
 '60.1327212117',
 '65.6017359512866',
 '99.416690000063',
 '98.2743453702088',
 '98.1365448192491',
 '98.2324938466415',
 '56.2251693973526',
 '89.570092140798',
 '44.8163768347735',
 '90.6238225668445',
 '97.2914835513583',
 '96.8500869295379',
 '96.8125528853954',
 '94.8751847446865',
 '94.5025759531236',
 '85.849707571326',
 '99.9987681484921',
 '50.5238061632824',
 '93.8179009205047',
 '98.786423758525',
 '50.5800665756201',
 '50.5759132657492',
 '99.4083794765038',
 '91.4203322426229',
 '48.5',
 '19.5',
 '10.3',
 '11.7',
 '82.3',
 '55',
 '93.5',
 '37.4',
 '76',
 '36.1',
 '14',
 '33.2',
 '78',
 '67.9',
 '48.9',
 '57.2',
 '47.1',
 '47.8',
 '50.1',
 '48.7119945935567',
 '55.4375772541101',
 '91.0264045161148',
 '97.5932932895718',
 '95.1851634429426',
 '98.2132410752903',
 '98.026194403413',
 '98.563293539577',
 '99.9977236468108',
 '99.9914644530224',
 '99.9954069607762',
 '57.8181418204066',
 '48.6553933920956',
 '99.9798069474748',
 '98.9771817674784',
 '90.0236427088539',
 '77.2202249370555',
 '60.6234216234335',
 '66.1197318922923',
 '99.4825175455422',
 '98.5791344954591',
 '98.4762455431909',
 '98.5451250779113',
 '56.8264012842674',
 '89.5002576329068',
 '44.9483258927663',
 '90.6193347387435',
 '97.3733015431867',
 '96.9464771200397',
 '96.9097460346813',
 '94.8855561460311',
 '94.6027752416708',
 '85.8227472405466',
 '51.1203418281979',
 '93.9107043828779',
 '98.2879959489889',
 '51.4347389951457',
 '51.4299317538115',
 '99.4851190889072',
 '91.3525709654628']
electricity_by_year["Access to elec."].replace({"..": ""}).unique()
array(['', '100', '95.7630462646484', '92.1548004150391',
       '99.9413986206055', '99.9842529296875', '87.475116',
       '94.606559753418', '99.846923828125', '92.257427', '89.9',
       '99.8443069458008', '70.334586', '96.8', '99.8828430175781',
       '99.8492813110352', '99.0415649414063', '98.9990844726563', '27.3',
       '99.8532638549805', '96.6253509521484', '70.19',
       '99.9834289550781', '91.3068161010742', '32.8', '88.7', '13.9',
       '99.8425200685198', '99.546800179424', '99.9943911362469', nan,
       '4.4', '99.4', '98.6', '24.2', '97.7', '95.7', '98.9', '91.7',
       '32', '89', '79', '21.5', '31.2', '70', '99', '26.5', '94.4',
       '97.1', '88.1', '9.1', '2.5', '58.6', '16.6', '41', '6', '3.1',
       '97.9', '96.7', '95.2', '39.8', '6.7', '29.4', '96.9', '48.7',
       '97', '56', '81.1', '88.8', '93.7', '84.5', '64.8', '29.3', '20.4',
       '12.7', '76.8', '73.6', '34.3', '99.8', '43.7', '99.9', '86',
       '73.3', '15.2', '74.6', '33.7', '67.4', '60.3', '86.3', '84.4',
       '99.6', '55.6', '42.6', '99.3', '4.3', '99.5', '12.6', '4.8',
       '83.8', '9.6', '68.6', '19.1', '98', '46', '98.7', '67.3', '99.1',
       '69.8', '6.1', '41.9', '36.5', '29.9', '73', '6.5', '43.2', '72.8',
       '81.4', '12.4', '89.6', '72.5', '74.7', '97.8', '6.2', '88',
       '52.9', '37.7', '94.1', '7.7', '4.7', '2.1', '72.4', '70.3',
       '95.4', '80', '23', '93.4', '98.5', '8.7', '82.1', '17.8', '17',
       '85.6', '91.3', '94.8', '95.6', '94.6', '7.4', '97.6', '22.2',
       '88.2', '99.7', '49.2', '16.7', '19.9638817610371',
       '34.1432102135773', '81.0997416649668', '87.0272667097087',
       '97.9262635329594', '67.5690834435773', '92.080656312843',
       '91.1581940859182', '91.1583141767936', '99.9939024980174',
       '99.1137058587738', '98.9925837200275', '98.2465834253865',
       '99.4761938755859', '40.5982967572402', '17.8675254053848',
       '99.4524626648336', '84.5263170160897', '74.0831790968894',
       '55.9714119039902', '26.4350562984302', '36.4025530411247',
       '95.2456141733554', '91.722504391963', '91.113646802215',
       '91.532905512629', '20.1822161091548', '72.8544678231575',
       '15.8054672531362', '58.2676706695706', '92.4180531531316',
       '91.426218615127', '91.3401715052384', '77.299823288762',
       '99.6359280517518', '72.4961541796735', '55.4438771570273',
       '99.7152423608883', '24.871639267962', '72.7326037651714',
       '57.891737765281', '25.659147015754', '25.6507452124749',
       '94.3274648640233', '78.3543617623921', '89.5', '62.4', '91.5',
       '34.1', '91.4', '90', '60', '99.2', '95.9', '19.2', '7', '85.3',
       '56.1', '56.8', '12', '8.4', '71.8', '13.5', '43.4', '61.9',
       '58.9', '95.1', '66', '44.6', '65', '27.2', '93', '87.1', '53',
       '78.3', '91', '85.5', '33.5', '17.2', '86.9', '39.6', '85.1',
       '93.3', '36', '83.4', '37.8', '82.5', '27.8', '9.4', '75.5',
       '11.9', '34.2', '38.8', '71.2', '97.3', '24.8', '52', '48.4',
       '84.9', '81.9', '15.7', '54.2', '90.1', '90.8', '18.1', '92.9',
       '89.8', '98.4', '19.8', '61', '18.8', '50.5', '51.4', '85.9',
       '92.7', '98.1', '44.9', '90.2', '23.5', '45.7', '49.1', '66.1',
       '27.9', '32.3', '31.860474023504', '47.6625364866446',
       '88.8342958618003', '93.9555303410912', '99.4060407563452',
       '84.7781290421301', '96.650227815738', '96.2870699100877',
       '97.0185492289121', '99.8754093344162', '99.7837597994349',
       '99.7496943982014', '99.8614239812103', '50.0334416807631',
       '35.3989999768092', '99.9189168958054', '94.4349904993138',
       '84.0374385425314', '68.7595094033684', '44.9427767431572',
       '52.9422008030466', '99.0133006644742', '97.0593672921663',
       '96.8125407490586', '97.0063562569368', '39.4566989775184',
       '83.1677876328261', '32.2789179055918', '78.3455492706838',
       '96.5203825283056', '95.9487475084581', '95.9037641046041',
       '88.5572255305389', '99.8482663987896', '87.0248640437107',
       '76.6814711878895', '99.9313827467578', '39.8441097568589',
       '86.7803650873228', '83.8523954854089', '38.2658690780816',
       '38.2601223073109', '98.8894942576766', '86.3012690468959', '71.5',
       '42', '74', '97.4', '91.8', '29.6', '62.1', '96.6', '16.2', '7.9',
       '86.4', '62.7', '98.2', '74.1', '44.3', '62.6', '59.1', '98.8',
       '45.8', '64.1', '29', '93.2', '87.3', '54.7', '90.5', '34.9',
       '20.1', '40.8', '97.5', '94.9', '41.6', '90.6', '40', '89.7',
       '31.8', '73.8', '23.8', '10.8', '37.6', '92.1', '39.5', '24',
       '60.5', '51.6', '82', '83.2', '52.5', '91.6', '18.4', '93.9',
       '89.1', '95.5', '22.8', '19.7', '55.1', '51.2', '94.3', '48', '95',
       '26.2', '44.7', '18.5', '52.3', '31.1', '33.9038001286462',
       '46.7587390583886', '89.0475351878897', '94.4839855823945',
       '99.5333634023846', '86.8527487873062', '96.9635036230764',
       '96.6357464510822', '97.342378290966', '99.1669776184071',
       '99.7676812016782', '98.3274195169609', '99.8916168370621',
       '50.0818317262279', '35.4839102740872', '99.4422835628403',
       '95.1884141896381', '84.8979365612511', '69.0584544888191',
       '47.0891947927519', '54.457463855061', '98.7891607198295',
       '97.2889505262466', '97.0711664557134', '97.2370514445961',
       '42.2813802769193', '84.1863196499328', '32.2126229767553',
       '80.7234409574285', '96.5086550115235', '95.9328003127985',
       '95.8874892892129', '89.763281693845', '99.8396896396459',
       '87.8757714925346', '78.703229504003', '99.9440320084058',
       '40.0606293618327', '87.7309920878869', '86.7867132149237',
       '39.1121832624488', '39.1065437139548', '98.9709975774677',
       '87.0237851218521', '41.8', '75.9', '37', '64.2', '8.5', '87.5',
       '68.8', '59.8', '13.4', '9.2', '76.4', '17.3', '45.3', '64.3',
       '96', '66.2', '46.9', '63.4', '42.9', '56.4', '79.3', '22.9',
       '40.4', '53.1', '92.6', '42.1', '35.1', '17.7', '72.2', '11',
       '41.2', '74.8', '49.7', '90.7', '83.9', '59.3', '92.4', '94.2',
       '92', '69.4', '64.5', '20.3', '58.2', '51.1', '5.3', '95.8',
       '76.5', '46.8', '26.7', '57.8', '68.9', '35.4', '42.5',
       '38.8546244221511', '50.906114634764', '89.5411657691967',
       '95.1697890618449', '99.6409785046505', '88.3686079048487',
       '97.0422752719492', '96.7243162467113', '97.4042780321237',
       '99.8283065055591', '99.8655891836026', '99.655469757283',
       '99.9170013923783', '54.1947420802944', '40.6329285874508',
       '99.8758820464439', '95.847095252009', '86.2839085579144',
       '72.8985169734875', '50.9990853826559', '58.3264142215853',
       '99.1246661085904', '97.4818550592041', '97.3029797168612',
       '97.4319920019967', '46.3321398827951', '85.5482587344368',
       '37.4463085105182', '82.7714915723345', '96.7983436224903',
       '96.276014130497', '96.2332941143712', '90.7857334721579',
       '99.89585109751', '89.1589196955869', '80.1272307017537',
       '99.9571187558814', '44.61180063733', '88.9341020251841',
       '88.9173147181527', '43.7370505625674', '43.7319091609899',
       '99.0052638487001', '88.1922255251927', '34.5', '9.3', '88.6',
       '60.9', '14.1', '10.9', '78.7', '18', '46.2', '65.6', '60.2',
       '66.4', '73.5', '56.2', '92.2', '26', '43.3', '86.5', '55.8',
       '85.7', '44.2', '93.6', '70.6', '24.1', '34.8', '93.8', '24.3',
       '86.6', '84.6', '17.6', '54.4', '74.3', '61.7', '23.4', '62.9',
       '52.1', '4.2', '32.1', '79.5', '32.4', '62.8', '79.2', '40.3',
       '44', '40.199898068195', '48.789457473528', '90.6663018742983',
       '95.7430331185145', '99.86914260178', '90.5262379043035',
       '97.6980233866031', '97.4517427779889', '98.1128558182722',
       '99.9637094037616', '99.9183821940095', '99.9272265326044',
       '99.9698703678352', '54.5631752651004', '41.5183905621843',
       '99.9608075777366', '96.6140192932522', '87.2637509200286',
       '71.9060547127056', '54.4361536322367', '60.2679332649365',
       '99.2795749258714', '97.7135942305631', '97.5717475090532',
       '97.6667483687171', '50.2107357057394', '86.565149962679',
       '39.0805495764912', '84.7280368393812', '97.6250026473811',
       '97.2291081354939', '97.1983752877486', '91.815222539659',
       '99.9405764596022', '90.4945894126707', '82.3564554155498',
       '99.970583083607', '44.320455294993', '90.2630998844596',
       '91.6760241733433', '43.6808105316438', '43.675736407086',
       '43.6808105316439', '99.1339177479467', '89.0186672372511', '39',
       '92.8', '68.3', '14.4', '9.5', '80.7', '62.2', '14.6', '10.1',
       '81', '18.7', '47', '67.1', '66.5', '44.8', '80.4', '92.3', '45',
       '28.4', '90.9', '44.4', '61.2', '46.3', '67', '50.9', '95.3',
       '43.6', '85.2', '56.5', '38', '71', '26.1', '65.8', '50.7', '84.7',
       '6.3', '55.3', '50', '61.6', '62', '40.2', '45.4',
       '43.0171479832647', '51.2110549404709', '89.1692812379426',
       '95.804767395244', '99.8729516393806', '92.0349140485874',
       '97.6650202588857', '97.4161347769004', '98.0496113702534',
       '99.9704727607712', '99.942320081113', '99.940819336018',
       '99.9708561296605', '54.5831226685803', '44.0132817192788',
       '99.9631057051384', '97.7587635009644', '88.2960951797823',
       '73.3946899985265', '55.3160844961813', '61.3394550392999',
       '99.2872327792157', '97.7603349993766', '97.5791568486266',
       '97.7119543642317', '51.1674138439625', '87.6492919680925',
       '40.4843611286372', '86.9830368829659', '96.1467646904579',
       '95.5104255149385', '95.4583357981029', '92.9486959090124',
       '99.8985605663644', '91.5254892263974', '82.9591610078136',
       '99.981741114911', '47.0762627878568', '91.0865817462377',
       '94.6179244479771', '46.3373545996798', '46.3326008959214',
       '99.152882923018', '89.8908858380532', '45.6', '70.1', '9.8', '84',
       '63.2', '14.3', '83.3', '47.7', '64.7', '66.6', '50.3', '77',
       '96.1', '90.3', '83.5', '42.2', '30.9', '91.1', '69.7', '89.3',
       '23.1', '68.5', '31', '11.2', '47.9', '80.1', '29.7', '53.3',
       '55.4', '23.6', '46.6', '75.1', '70.4', '22.7', '85', '6.8',
       '57.6', '89.2', '41.3', '43', '46.7', '44.3812594428681',
       '51.1680826551065', '90.3581538695401', '96.2215700753372',
       '99.9455010885556', '92.834365738276', '97.8779427927934',
       '97.6524316332846', '98.2621398882088', '99.9533142481858',
       '99.9663713381538', '99.9065322711927', '99.9875256636779',
       '55.5016312834383', '45.0522458898094', '99.9505379687558',
       '97.8867643059813', '88.6615853993001', '74.1134591811169',
       '57.0959550724622', '62.7544694323566', '99.3356536729549',
       '98.0015266427987', '97.8532110437475', '97.9559417265741',
       '52.9514648159524', '88.0490889564092', '41.836982681727',
       '87.6841442253686', '97.1324839563419', '96.6517404120541',
       '96.6125775683893', '93.3328680629391', '99.9400367116293',
       '92.7013041706379', '84.5931602439996', '99.9929550285056',
       '47.5782396795133', '92.2015549079081', '95.3232055481217',
       '47.1303990695105', '47.1257966456512', '99.2510521973761',
       '90.199100032655', '96.2', '97.2', '15.4', '69.9', '66.7', '85.4',
       '33.4', '93.1', '96.5', '47.5', '27.6', '11.5', '50.6', '98.3',
       '30.6', '94.5', '20.5', '96.4', '45.2', '26.3', '72.9', '49.9',
       '7.3', '59.7', '39.9', '54.1', '73.9', '52.7', '46.2648745577187',
       '51.7308990685072', '90.6384870529171', '97.0414890291467',
       '99.9796417892388', '93.4926075631317', '97.8013659805564',
       '97.5687165889393', '98.1511649991063', '99.9760236823625',
       '99.9777773685799', '99.9520178888209', '99.9953534951789',
       '56.346408400069', '46.3640414093557', '99.9686544956864',
       '98.0667929887089', '89.0003692130539', '74.6422206071065',
       '58.6069711033586', '63.9296623394879', '99.3926777630817',
       '98.152759448219', '98.0060017525721', '98.1092788328358',
       '54.562507330301', '88.4120504553677', '43.217947106415',
       '88.4522403939564', '97.2315412836112', '96.7733482603745',
       '96.7352691074454', '93.673300524618', '99.9332673681265',
       '93.6071297305702', '86.4225510641771', '99.9971273501603',
       '48.3855447902348', '93.2090070960632', '96.197039231936',
       '48.478195544667', '48.4737889046716', '99.1899480241711',
       '90.4854035957629', '48.2', '73.7', '19', '10.2', '65.4', '11.3',
       '87.9', '20.8', '71.1', '66.8', '82.9', '63.7', '35.8', '47.2',
       '52.6', '50.4', '29.8', '70.2', '14.2', '53.4', '83.6', '31.5',
       '55.2', '18.6', '59.5', '20.9', '78.5', '68', '27.5', '76.3',
       '49.3', '61.8', '42.7', '55.7', '74.9', '49', '48.100862134193',
       '54.224724307904', '90.8493820461977', '97.3939355970029',
       '99.9864361919036', '95.2851631707137', '98.1555450173615',
       '97.9617970162443', '98.5234655577502', '99.9881579022417',
       '99.9616380974146', '99.9763188018388', '99.9969239356276',
       '57.8615299350207', '48.0509504079887', '99.9798902530907',
       '99.1169517100785', '90.1059784759654', '76.6323166518695',
       '60.1327212117', '65.6017359512866', '99.416690000063',
       '98.2743453702088', '98.1365448192491', '98.2324938466415',
       '56.2251693973526', '89.570092140798', '44.8163768347735',
       '90.6238225668445', '97.2914835513583', '96.8500869295379',
       '96.8125528853954', '94.8751847446865', '94.5025759531236',
       '85.849707571326', '99.9987681484921', '50.5238061632824',
       '93.8179009205047', '98.786423758525', '50.5800665756201',
       '50.5759132657492', '99.4083794765038', '91.4203322426229', '48.5',
       '19.5', '10.3', '11.7', '82.3', '55', '93.5', '37.4', '76', '36.1',
       '14', '33.2', '78', '67.9', '48.9', '57.2', '47.1', '47.8', '50.1',
       '48.7119945935567', '55.4375772541101', '91.0264045161148',
       '97.5932932895718', '95.1851634429426', '98.2132410752903',
       '98.026194403413', '98.563293539577', '99.9977236468108',
       '99.9914644530224', '99.9954069607762', '57.8181418204066',
       '48.6553933920956', '99.9798069474748', '98.9771817674784',
       '90.0236427088539', '77.2202249370555', '60.6234216234335',
       '66.1197318922923', '99.4825175455422', '98.5791344954591',
       '98.4762455431909', '98.5451250779113', '56.8264012842674',
       '89.5002576329068', '44.9483258927663', '90.6193347387435',
       '97.3733015431867', '96.9464771200397', '96.9097460346813',
       '94.8855561460311', '94.6027752416708', '85.8227472405466',
       '51.1203418281979', '93.9107043828779', '98.2879959489889',
       '51.4347389951457', '51.4299317538115', '99.4851190889072',
       '91.3525709654628'], dtype=object)
electricity_by_year["Access to elec."] = electricity_by_year["Access to elec."].replace({"..": None}).astype(float)
px.line(
    electricity_by_year,
    x="Year",
    y="Access to elec.",
    color="Country Name",
    title="Access to electricity for each country by year",
)

Filter#

electricity_by_year.head()
Series Name Series Code Country Name Country Code Year Access to elec.
0 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 1990 [YR1990] NaN
1 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 1990 [YR1990] 100.0
2 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 1990 [YR1990] NaN
3 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 1990 [YR1990] NaN
4 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 1990 [YR1990] 100.0
first_country_names = electricity_by_year["Country Name"].head()
first_country_names
0       Afghanistan
1           Albania
2           Algeria
3    American Samoa
4           Andorra
Name: Country Name, dtype: object
is_first_country = electricity_by_year["Country Name"].isin(first_country_names)
is_first_country
0        True
1        True
2        True
3        True
4        True
        ...  
3247    False
3248    False
3249    False
3250    False
3251    False
Name: Country Name, Length: 3252, dtype: bool
first_countries = electricity_by_year[is_first_country]
first_countries
Series Name Series Code Country Name Country Code Year Access to elec.
0 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 1990 [YR1990] NaN
1 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 1990 [YR1990] 100.0
2 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 1990 [YR1990] NaN
3 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 1990 [YR1990] NaN
4 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 1990 [YR1990] 100.0
271 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2000 [YR2000] 4.4
272 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2000 [YR2000] 99.4
273 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2000 [YR2000] 98.6
274 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2000 [YR2000] NaN
275 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2000 [YR2000] 100.0
542 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2014 [YR2014] 89.5
543 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2014 [YR2014] 100.0
544 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2014 [YR2014] 99.3
545 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2014 [YR2014] NaN
546 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2014 [YR2014] 100.0
813 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2015 [YR2015] 71.5
814 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2015 [YR2015] 100.0
815 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2015 [YR2015] 99.4
816 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2015 [YR2015] NaN
817 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2015 [YR2015] 100.0
1084 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2016 [YR2016] 97.7
1085 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2016 [YR2016] 99.9
1086 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2016 [YR2016] 99.4
1087 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2016 [YR2016] NaN
1088 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2016 [YR2016] 100.0
1355 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2017 [YR2017] 97.7
1356 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2017 [YR2017] 99.9
1357 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2017 [YR2017] 99.5
1358 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2017 [YR2017] NaN
1359 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2017 [YR2017] 100.0
1626 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2018 [YR2018] 93.4
1627 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2018 [YR2018] 100.0
1628 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2018 [YR2018] 99.6
1629 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2018 [YR2018] NaN
1630 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2018 [YR2018] 100.0
1897 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2019 [YR2019] 97.7
1898 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2019 [YR2019] 100.0
1899 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2019 [YR2019] 99.5
1900 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2019 [YR2019] NaN
1901 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2019 [YR2019] 100.0
2168 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2020 [YR2020] 97.7
2169 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2020 [YR2020] 100.0
2170 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2020 [YR2020] 99.7
2171 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2020 [YR2020] NaN
2172 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2020 [YR2020] 100.0
2439 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2021 [YR2021] 97.7
2440 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2021 [YR2021] 100.0
2441 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2021 [YR2021] 99.8
2442 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2021 [YR2021] NaN
2443 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2021 [YR2021] 100.0
2710 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2022 [YR2022] 85.3
2711 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2022 [YR2022] 100.0
2712 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2022 [YR2022] 100.0
2713 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2022 [YR2022] NaN
2714 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2022 [YR2022] 100.0
2981 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2023 [YR2023] NaN
2982 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2023 [YR2023] NaN
2983 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2023 [YR2023] NaN
2984 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2023 [YR2023] NaN
2985 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2023 [YR2023] NaN
px.line(
    first_countries,
    x="Year",
    y="Access to elec.",
    color="Country Name",
    title="Access to electricity for each country by year, first five",
)

Happiness data#

happiness = pd.read_excel("data/DataForTable2.1.xls")
happiness
Country name year Life Ladder Log GDP per capita Social support Healthy life expectancy at birth Freedom to make life choices Generosity Perceptions of corruption Positive affect Negative affect
0 Afghanistan 2008 3.723590 7.350416 0.450662 50.500000 0.718114 0.164055 0.881686 0.414297 0.258195
1 Afghanistan 2009 4.401778 7.508646 0.552308 50.799999 0.678896 0.187297 0.850035 0.481421 0.237092
2 Afghanistan 2010 4.758381 7.613900 0.539075 51.099998 0.600127 0.117861 0.706766 0.516907 0.275324
3 Afghanistan 2011 3.831719 7.581259 0.521104 51.400002 0.495901 0.160098 0.731109 0.479835 0.267175
4 Afghanistan 2012 3.782938 7.660506 0.520637 51.700001 0.530935 0.234157 0.775620 0.613513 0.267919
... ... ... ... ... ... ... ... ... ... ... ...
2358 Zimbabwe 2019 2.693523 7.697755 0.759162 53.099998 0.631908 -0.050874 0.830652 0.658434 0.235354
2359 Zimbabwe 2020 3.159802 7.596050 0.717243 53.575001 0.643303 0.002848 0.788523 0.660658 0.345736
2360 Zimbabwe 2021 3.154578 7.656878 0.685151 54.049999 0.667636 -0.079007 0.756945 0.609917 0.241682
2361 Zimbabwe 2022 3.296220 7.670073 0.666172 54.525002 0.651987 -0.072935 0.752632 0.640609 0.191350
2362 Zimbabwe 2023 3.572386 7.678590 0.693817 55.000000 0.734613 -0.068837 0.757494 0.609752 0.178953

2363 rows × 11 columns

px.scatter(
    x=electricity_by_year["Access to elec."],
    y=happiness["Life Ladder"],
    title="Country access to electricity vs. happiness",
)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[17], line 1
----> 1 px.scatter(
      2     x=electricity_by_year["Access to elec."],
      3     y=happiness["Life Ladder"],
      4     title="Country access to electricity vs. happiness"
      5 )

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/plotly/express/_chart_types.py:66, in scatter(data_frame, x, y, color, symbol, size, hover_name, hover_data, custom_data, text, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, orientation, color_discrete_sequence, color_discrete_map, color_continuous_scale, range_color, color_continuous_midpoint, symbol_sequence, symbol_map, opacity, size_max, marginal_x, marginal_y, trendline, trendline_options, trendline_color_override, trendline_scope, log_x, log_y, range_x, range_y, render_mode, title, template, width, height)
     12 def scatter(
     13     data_frame=None,
     14     x=None,
   (...)
     60     height=None,
     61 ) -> go.Figure:
     62     """
     63     In a scatter plot, each row of `data_frame` is represented by a symbol
     64     mark in 2D space.
     65     """
---> 66     return make_figure(args=locals(), constructor=go.Scatter)

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/plotly/express/_core.py:2117, in make_figure(args, constructor, trace_patch, layout_patch)
   2114 layout_patch = layout_patch or {}
   2115 apply_default_cascade(args)
-> 2117 args = build_dataframe(args, constructor)
   2118 if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
   2119     args = process_dataframe_hierarchy(args)

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/plotly/express/_core.py:1513, in build_dataframe(args, constructor)
   1510     args["color"] = None
   1511 # now that things have been prepped, we do the systematic rewriting of `args`
-> 1513 df_output, wide_id_vars = process_args_into_dataframe(
   1514     args, wide_mode, var_name, value_name
   1515 )
   1517 # now that `df_output` exists and `args` contains only references, we complete
   1518 # the special-case and wide-mode handling by further rewriting args and/or mutating
   1519 # df_output
   1521 count_name = _escape_col_name(df_output, "count", [var_name, value_name])

File /opt/homebrew/Caskroom/mambaforge/base/envs/computing-in-context/lib/python3.12/site-packages/plotly/express/_core.py:1274, in process_args_into_dataframe(args, wide_mode, var_name, value_name)
   1271         col_name = _check_name_not_reserved(field, reserved_names)
   1273     if length and len(argument) != length:
-> 1274         raise ValueError(
   1275             "All arguments should have the same length. "
   1276             "The length of argument `%s` is %d, whereas the "
   1277             "length of  previously-processed arguments %s is %d"
   1278             % (field, len(argument), str(list(df_output.keys())), length)
   1279         )
   1280     df_output[str(col_name)] = to_unindexed_series(argument, str(col_name))
   1282 # Finally, update argument with column name now that column exists

ValueError: All arguments should have the same length. The length of argument `y` is 2363, whereas the length of  previously-processed arguments ['x'] is 3252
len(electricity_by_year["Access to elec."])
3252
len(happiness["Life Ladder"])
2363
electricity_by_year["Year"]
0       1990 [YR1990]
1       1990 [YR1990]
2       1990 [YR1990]
3       1990 [YR1990]
4       1990 [YR1990]
            ...      
3247    2023 [YR2023]
3248    2023 [YR2023]
3249    2023 [YR2023]
3250    2023 [YR2023]
3251    2023 [YR2023]
Name: Year, Length: 3252, dtype: object
happiness["year"]
0       2008
1       2009
2       2010
3       2011
4       2012
        ... 
2358    2019
2359    2020
2360    2021
2361    2022
2362    2023
Name: year, Length: 2363, dtype: int64

We decided to diverge from the original prompt and make a scatter plot.

Merge#

elec_2023 = electricity_by_year[electricity_by_year["Year"] == "2023 [YR2023]"]
elec_2023
Series Name Series Code Country Name Country Code Year Access to elec.
2981 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2023 [YR2023] NaN
2982 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2023 [YR2023] NaN
2983 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2023 [YR2023] NaN
2984 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2023 [YR2023] NaN
2985 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2023 [YR2023] NaN
... ... ... ... ... ... ...
3247 NaN NaN NaN NaN 2023 [YR2023] NaN
3248 NaN NaN NaN NaN 2023 [YR2023] NaN
3249 NaN NaN NaN NaN 2023 [YR2023] NaN
3250 Data from database: World Development Indicators NaN NaN NaN 2023 [YR2023] NaN
3251 Last Updated: 10/24/2024 NaN NaN NaN 2023 [YR2023] NaN

271 rows × 6 columns

hap_2023 = happiness[happiness["year"] == 2023]
hap_2023
Country name year Life Ladder Log GDP per capita Social support Healthy life expectancy at birth Freedom to make life choices Generosity Perceptions of corruption Positive affect Negative affect
14 Afghanistan 2023 1.445909 NaN 0.368478 55.200001 0.228301 NaN 0.738471 0.260513 0.460167
30 Albania 2023 5.444691 9.688706 0.690753 69.199997 0.871545 0.067885 0.855425 0.597349 0.314227
63 Argentina 2023 6.393229 9.993596 0.892118 67.300003 0.831684 -0.129061 0.846094 0.720122 0.301162
80 Armenia 2023 5.679090 9.729613 0.819338 68.199997 0.819376 -0.179444 0.680709 0.574717 0.422631
97 Australia 2023 7.024582 10.846434 0.896460 71.199997 0.875769 0.187309 0.481580 0.731053 0.248163
... ... ... ... ... ... ... ... ... ... ... ...
2295 Venezuela 2023 5.765363 NaN 0.884668 63.700001 0.756530 NaN 0.825393 0.757685 0.300038
2313 Vietnam 2023 6.325115 9.392351 0.844905 65.699997 0.955560 -0.159114 0.655275 0.710068 0.119510
2327 Yemen 2023 3.531574 NaN 0.824958 56.599998 0.582724 NaN 0.771464 0.446534 0.340794
2344 Zambia 2023 3.685568 8.115053 0.664099 56.099998 0.854004 0.092377 0.814191 0.652999 0.359132
2362 Zimbabwe 2023 3.572386 7.678590 0.693817 55.000000 0.734613 -0.068837 0.757494 0.609752 0.178953

138 rows × 11 columns

merged = pd.merge(elec_2023, hap_2023, left_on="Country Name", right_on="Country name")
merged
Series Name Series Code Country Name Country Code Year Access to elec. Country name year Life Ladder Log GDP per capita Social support Healthy life expectancy at birth Freedom to make life choices Generosity Perceptions of corruption Positive affect Negative affect
0 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2023 [YR2023] NaN Afghanistan 2023 1.445909 NaN 0.368478 55.200001 0.228301 NaN 0.738471 0.260513 0.460167
1 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2023 [YR2023] NaN Albania 2023 5.444691 9.688706 0.690753 69.199997 0.871545 0.067885 0.855425 0.597349 0.314227
2 Access to electricity (% of population) EG.ELC.ACCS.ZS Argentina ARG 2023 [YR2023] NaN Argentina 2023 6.393229 9.993596 0.892118 67.300003 0.831684 -0.129061 0.846094 0.720122 0.301162
3 Access to electricity (% of population) EG.ELC.ACCS.ZS Armenia ARM 2023 [YR2023] NaN Armenia 2023 5.679090 9.729613 0.819338 68.199997 0.819376 -0.179444 0.680709 0.574717 0.422631
4 Access to electricity (% of population) EG.ELC.ACCS.ZS Australia AUS 2023 [YR2023] NaN Australia 2023 7.024582 10.846434 0.896460 71.199997 0.875769 0.187309 0.481580 0.731053 0.248163
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
116 Access to electricity (% of population) EG.ELC.ACCS.ZS United States USA 2023 [YR2023] NaN United States 2023 6.520872 11.089293 0.860503 65.599998 0.721453 0.184887 0.721701 0.706368 0.283993
117 Access to electricity (% of population) EG.ELC.ACCS.ZS Uruguay URY 2023 [YR2023] NaN Uruguay 2023 6.661722 10.122264 0.908374 67.500000 0.904164 -0.050256 0.661658 0.753448 0.264817
118 Access to electricity (% of population) EG.ELC.ACCS.ZS Uzbekistan UZB 2023 [YR2023] NaN Uzbekistan 2023 6.385361 9.025871 0.908859 65.900002 0.926784 0.247137 0.649540 0.751747 0.202207
119 Access to electricity (% of population) EG.ELC.ACCS.ZS Zambia ZMB 2023 [YR2023] NaN Zambia 2023 3.685568 8.115053 0.664099 56.099998 0.854004 0.092377 0.814191 0.652999 0.359132
120 Access to electricity (% of population) EG.ELC.ACCS.ZS Zimbabwe ZWE 2023 [YR2023] NaN Zimbabwe 2023 3.572386 7.678590 0.693817 55.000000 0.734613 -0.068837 0.757494 0.609752 0.178953

121 rows × 17 columns

Plot#

px.scatter(
    merged,
    x="Access to elec.",
    y="Life Ladder",
    color="Country Name",
    title="Country access to electricity vs. happiness",
)
merged.dtypes
Series Name                          object
Series Code                          object
Country Name                         object
Country Code                         object
Year                                 object
Access to elec.                     float64
Country name                         object
year                                  int64
Life Ladder                         float64
Log GDP per capita                  float64
Social support                      float64
Healthy life expectancy at birth    float64
Freedom to make life choices        float64
Generosity                          float64
Perceptions of corruption           float64
Positive affect                     float64
Negative affect                     float64
dtype: object
merged[["Country Name", "Access to elec.", "Life Ladder"]]
Country Name Access to elec. Life Ladder
0 Afghanistan NaN 1.445909
1 Albania NaN 5.444691
2 Argentina NaN 6.393229
3 Armenia NaN 5.679090
4 Australia NaN 7.024582
... ... ... ...
116 United States NaN 6.520872
117 Uruguay NaN 6.661722
118 Uzbekistan NaN 6.385361
119 Zambia NaN 3.685568
120 Zimbabwe NaN 3.572386

121 rows × 3 columns

merged2 = pd.merge(elec_2023, hap_2023, left_on="Country Name", right_on="Country name", how="inner")
merged2 = merged2[["Country Name", "Access to elec.", "Life Ladder"]]
merged2
Country Name Access to elec. Life Ladder
0 Afghanistan NaN 1.445909
1 Albania NaN 5.444691
2 Argentina NaN 6.393229
3 Armenia NaN 5.679090
4 Australia NaN 7.024582
... ... ... ...
116 United States NaN 6.520872
117 Uruguay NaN 6.661722
118 Uzbekistan NaN 6.385361
119 Zambia NaN 3.685568
120 Zimbabwe NaN 3.572386

121 rows × 3 columns

electricity["2023 [YR2023]"].unique()
array(['..', nan], dtype=object)

Whoops, 2023 is all blank. We need to do a different year.

2022#

elec_2022 = electricity_by_year[electricity_by_year["Year"] == "2022 [YR2022]"]
elec_2022
Series Name Series Code Country Name Country Code Year Access to elec.
2710 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan AFG 2022 [YR2022] 85.3
2711 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania ALB 2022 [YR2022] 100.0
2712 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria DZA 2022 [YR2022] 100.0
2713 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa ASM 2022 [YR2022] NaN
2714 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra AND 2022 [YR2022] 100.0
... ... ... ... ... ... ...
2976 NaN NaN NaN NaN 2022 [YR2022] NaN
2977 NaN NaN NaN NaN 2022 [YR2022] NaN
2978 NaN NaN NaN NaN 2022 [YR2022] NaN
2979 Data from database: World Development Indicators NaN NaN NaN 2022 [YR2022] NaN
2980 Last Updated: 10/24/2024 NaN NaN NaN 2022 [YR2022] NaN

271 rows × 6 columns

hap_2022 = happiness[happiness["year"] == 2022]
hap_2022
Country name year Life Ladder Log GDP per capita Social support Healthy life expectancy at birth Freedom to make life choices Generosity Perceptions of corruption Positive affect Negative affect
13 Afghanistan 2022 1.281271 NaN 0.228217 54.875000 0.368377 NaN 0.733198 0.205868 0.575512
29 Albania 2022 5.212213 9.648703 0.724090 69.175003 0.802250 -0.069952 0.845502 0.547126 0.254826
41 Algeria 2022 5.538172 9.322542 0.783401 66.699997 0.439971 -0.044912 0.611083 0.583429 0.258596
62 Argentina 2022 6.260993 10.018916 0.893330 67.250000 0.825189 -0.130209 0.810037 0.724068 0.284279
79 Armenia 2022 5.381943 9.682955 0.811169 67.925003 0.789599 -0.157764 0.704730 0.530931 0.549468
... ... ... ... ... ... ... ... ... ... ... ...
2294 Venezuela 2022 5.948992 NaN 0.899366 63.875000 0.770417 NaN 0.798016 0.754337 0.292252
2312 Vietnam 2022 6.266509 9.341064 0.878744 65.599998 0.975405 -0.182106 0.703423 0.774236 0.108473
2326 Yemen 2022 3.590379 NaN 0.872113 56.825001 0.606788 NaN 0.787555 0.459952 0.255151
2343 Zambia 2022 3.728098 8.101115 0.717196 55.674999 0.888741 -0.008760 0.716396 0.659846 0.308741
2361 Zimbabwe 2022 3.296220 7.670073 0.666172 54.525002 0.651987 -0.072935 0.752632 0.640609 0.191350

140 rows × 11 columns

merged_2022 = pd.merge(elec_2022, hap_2022, left_on="Country Name", right_on="Country name", how="inner")
merged_2022 = merged_2022[["Country Name", "Access to elec.", "Life Ladder"]]
merged_2022
Country Name Access to elec. Life Ladder
0 Afghanistan 85.3 1.281271
1 Albania 100.0 5.212213
2 Algeria 100.0 5.538172
3 Argentina 100.0 6.260993
4 Armenia 100.0 5.381943
... ... ... ...
117 United States 100.0 6.692790
118 Uruguay 100.0 6.670853
119 Uzbekistan 100.0 6.016239
120 Zambia 47.8 3.728098
121 Zimbabwe 50.1 3.296220

122 rows × 3 columns

Plot#

px.scatter(
    merged_2022,
    x="Access to elec.",
    y="Life Ladder",
    color="Country Name",
    title="Country access to electricity vs. happiness",
)