In [2]:
import pandas as pd
In [3]:
europe = pd.read_csv('data/gapminder_gdp_europe.csv', index_col='country')
In [7]:
europe.describe()
Out[7]:
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
gdpPercap_2002
gdpPercap_2007
count
30.000000
30.000000
30.000000
30.000000
30.000000
30.000000
30.000000
30.000000
30.000000
30.000000
30.000000
30.000000
mean
5661.057435
6963.012816
8365.486814
10143.823757
12479.575246
14283.979110
15617.896551
17214.310727
17061.568084
19076.781802
21711.732422
25054.481636
std
3114.060493
3677.950146
4199.193906
4724.983889
5509.691411
5874.464896
6453.234827
7482.957960
9109.804361
10065.457716
11197.355517
11800.339811
min
973.533195
1353.989176
1709.683679
2172.352423
2860.169750
3528.481305
3630.880722
3738.932735
2497.437901
3193.054604
4604.211737
5937.029526
25%
3241.132406
4394.874315
5373.536612
6657.939047
9057.708095
10360.030300
11449.870115
12274.570680
8667.113214
9946.599306
11721.851483
14811.898210
50%
5142.469716
6066.721495
7515.733738
9366.067033
12326.379990
14225.754515
15322.824720
16215.485895
17550.155945
19596.498550
23674.863230
28054.065790
75%
7236.794919
9597.220820
10931.085347
13277.182057
16523.017127
19052.412163
20901.729730
23321.587723
25034.243045
27189.530312
30373.363307
33817.962533
max
14734.232750
17909.489730
20431.092700
22966.144320
27195.113040
26982.290520
28397.715120
31540.974800
33965.661150
41283.164330
44683.975250
49357.190170
In [8]:
europe.head(3)
Out[8]:
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
gdpPercap_2002
gdpPercap_2007
country
Albania
1601.056136
1942.284244
2312.888958
2760.196931
3313.422188
3533.00391
3630.880722
3738.932735
2497.437901
3193.054604
4604.211737
5937.029526
Austria
6137.076492
8842.598030
10750.721110
12834.602400
16661.625600
19749.42230
21597.083620
23687.826070
27042.018680
29095.920660
32417.607690
36126.492700
Belgium
8343.105127
9714.960623
10991.206760
13149.041190
16672.143560
19117.97448
20979.845890
22525.563080
25575.570690
27561.196630
30485.883750
33692.605080
In [9]:
europe.iloc[0,0]
Out[9]:
1601.056136
In [10]:
europe.loc['Albania', 'gdpPercap_1952']
Out[10]:
1601.056136
In [13]:
europe.loc['Germany',:]
Out[13]:
gdpPercap_1952 7144.114393
gdpPercap_1957 10187.826650
gdpPercap_1962 12902.462910
gdpPercap_1967 14745.625610
gdpPercap_1972 18016.180270
gdpPercap_1977 20512.921230
gdpPercap_1982 22031.532740
gdpPercap_1987 24639.185660
gdpPercap_1992 26505.303170
gdpPercap_1997 27788.884160
gdpPercap_2002 30035.801980
gdpPercap_2007 32170.374420
Name: Germany, dtype: float64
In [14]:
europe.loc[:,'gdpPercap_2007']
Out[14]:
country
Albania 5937.029526
Austria 36126.492700
Belgium 33692.605080
Bosnia and Herzegovina 7446.298803
Bulgaria 10680.792820
Croatia 14619.222720
Czech Republic 22833.308510
Denmark 35278.418740
Finland 33207.084400
France 30470.016700
Germany 32170.374420
Greece 27538.411880
Hungary 18008.944440
Iceland 36180.789190
Ireland 40675.996350
Italy 28569.719700
Montenegro 9253.896111
Netherlands 36797.933320
Norway 49357.190170
Poland 15389.924680
Portugal 20509.647770
Romania 10808.475610
Serbia 9786.534714
Slovak Republic 18678.314350
Slovenia 25768.257590
Spain 28821.063700
Sweden 33859.748350
Switzerland 37506.419070
Turkey 8458.276384
United Kingdom 33203.261280
Name: gdpPercap_2007, dtype: float64
In [16]:
dir(europe)
Out[16]:
['T',
'_AXIS_ALIASES',
'_AXIS_IALIASES',
'_AXIS_LEN',
'_AXIS_NAMES',
'_AXIS_NUMBERS',
'_AXIS_ORDERS',
'_AXIS_REVERSED',
'_AXIS_SLICEMAP',
'__abs__',
'__add__',
'__and__',
'__array__',
'__array_wrap__',
'__bool__',
'__bytes__',
'__class__',
'__contains__',
'__copy__',
'__deepcopy__',
'__delattr__',
'__delitem__',
'__dict__',
'__dir__',
'__div__',
'__doc__',
'__eq__',
'__finalize__',
'__floordiv__',
'__format__',
'__ge__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__getstate__',
'__gt__',
'__hash__',
'__iadd__',
'__iand__',
'__ifloordiv__',
'__imod__',
'__imul__',
'__init__',
'__init_subclass__',
'__invert__',
'__ior__',
'__ipow__',
'__isub__',
'__iter__',
'__itruediv__',
'__ixor__',
'__le__',
'__len__',
'__lt__',
'__matmul__',
'__mod__',
'__module__',
'__mul__',
'__ne__',
'__neg__',
'__new__',
'__nonzero__',
'__or__',
'__pos__',
'__pow__',
'__radd__',
'__rand__',
'__rdiv__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rfloordiv__',
'__rmatmul__',
'__rmod__',
'__rmul__',
'__ror__',
'__round__',
'__rpow__',
'__rsub__',
'__rtruediv__',
'__rxor__',
'__setattr__',
'__setitem__',
'__setstate__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__truediv__',
'__unicode__',
'__weakref__',
'__xor__',
'_accessors',
'_add_numeric_operations',
'_add_series_only_operations',
'_add_series_or_dataframe_operations',
'_agg_by_level',
'_agg_doc',
'_aggregate',
'_aggregate_multiple_funcs',
'_align_frame',
'_align_series',
'_box_col_values',
'_box_item_values',
'_builtin_table',
'_check_inplace_setting',
'_check_is_chained_assignment_possible',
'_check_label_or_level_ambiguity',
'_check_percentile',
'_check_setitem_copy',
'_clear_item_cache',
'_clip_with_one_bound',
'_clip_with_scalar',
'_combine_const',
'_combine_frame',
'_combine_match_columns',
'_combine_match_index',
'_compare_frame',
'_consolidate',
'_consolidate_inplace',
'_construct_axes_dict',
'_construct_axes_dict_for_slice',
'_construct_axes_dict_from',
'_construct_axes_from_arguments',
'_constructor',
'_constructor_expanddim',
'_constructor_sliced',
'_convert',
'_count_level',
'_create_indexer',
'_cython_table',
'_deprecations',
'_dir_additions',
'_dir_deletions',
'_drop_axis',
'_drop_labels_or_levels',
'_ensure_valid_index',
'_expand_axes',
'_find_valid_index',
'_from_arrays',
'_from_axes',
'_get_agg_axis',
'_get_axis',
'_get_axis_name',
'_get_axis_number',
'_get_axis_resolvers',
'_get_block_manager_axis',
'_get_bool_data',
'_get_cacher',
'_get_index_resolvers',
'_get_item_cache',
'_get_label_or_level_values',
'_get_numeric_data',
'_get_value',
'_get_values',
'_getitem_array',
'_getitem_column',
'_getitem_frame',
'_getitem_multilevel',
'_getitem_slice',
'_gotitem',
'_iget_item_cache',
'_indexed_same',
'_info_axis',
'_info_axis_name',
'_info_axis_number',
'_info_repr',
'_init_dict',
'_init_mgr',
'_init_ndarray',
'_internal_names',
'_internal_names_set',
'_is_builtin_func',
'_is_cached',
'_is_copy',
'_is_cython_func',
'_is_datelike_mixed_type',
'_is_label_or_level_reference',
'_is_label_reference',
'_is_level_reference',
'_is_mixed_type',
'_is_numeric_mixed_type',
'_is_view',
'_ix',
'_ixs',
'_join_compat',
'_maybe_cache_changed',
'_maybe_update_cacher',
'_metadata',
'_needs_reindex_multi',
'_obj_with_exclusions',
'_protect_consolidate',
'_reduce',
'_reindex_axes',
'_reindex_axis',
'_reindex_columns',
'_reindex_index',
'_reindex_multi',
'_reindex_with_indexers',
'_repr_data_resource_',
'_repr_fits_horizontal_',
'_repr_fits_vertical_',
'_repr_html_',
'_repr_latex_',
'_reset_cache',
'_reset_cacher',
'_sanitize_column',
'_selected_obj',
'_selection',
'_selection_list',
'_selection_name',
'_series',
'_set_as_cached',
'_set_axis',
'_set_axis_name',
'_set_is_copy',
'_set_item',
'_set_value',
'_setitem_array',
'_setitem_frame',
'_setitem_slice',
'_setup_axes',
'_shallow_copy',
'_slice',
'_stat_axis',
'_stat_axis_name',
'_stat_axis_number',
'_take',
'_to_dict_of_blocks',
'_try_aggregate_string_function',
'_typ',
'_unpickle_frame_compat',
'_unpickle_matrix_compat',
'_update_inplace',
'_validate_dtype',
'_values',
'_where',
'_xs',
'abs',
'add',
'add_prefix',
'add_suffix',
'agg',
'aggregate',
'align',
'all',
'any',
'append',
'apply',
'applymap',
'as_matrix',
'asfreq',
'asof',
'assign',
'astype',
'at',
'at_time',
'axes',
'between_time',
'bfill',
'bool',
'boxplot',
'clip',
'clip_lower',
'clip_upper',
'columns',
'combine',
'combine_first',
'compound',
'copy',
'corr',
'corrwith',
'count',
'cov',
'cummax',
'cummin',
'cumprod',
'cumsum',
'describe',
'diff',
'div',
'divide',
'dot',
'drop',
'drop_duplicates',
'dropna',
'dtypes',
'duplicated',
'empty',
'eq',
'equals',
'eval',
'ewm',
'expanding',
'ffill',
'fillna',
'filter',
'first',
'first_valid_index',
'floordiv',
'from_dict',
'from_records',
'ftypes',
'gdpPercap_1952',
'gdpPercap_1957',
'gdpPercap_1962',
'gdpPercap_1967',
'gdpPercap_1972',
'gdpPercap_1977',
'gdpPercap_1982',
'gdpPercap_1987',
'gdpPercap_1992',
'gdpPercap_1997',
'gdpPercap_2002',
'gdpPercap_2007',
'ge',
'get',
'get_dtype_counts',
'get_ftype_counts',
'get_values',
'groupby',
'gt',
'head',
'hist',
'iat',
'idxmax',
'idxmin',
'iloc',
'index',
'infer_objects',
'info',
'insert',
'interpolate',
'isin',
'isna',
'isnull',
'items',
'iteritems',
'iterrows',
'itertuples',
'ix',
'join',
'keys',
'kurt',
'kurtosis',
'last',
'last_valid_index',
'le',
'loc',
'lookup',
'lt',
'mad',
'mask',
'max',
'mean',
'median',
'melt',
'memory_usage',
'merge',
'min',
'mod',
'mode',
'mul',
'multiply',
'ndim',
'ne',
'nlargest',
'notna',
'notnull',
'nsmallest',
'nunique',
'pct_change',
'pipe',
'pivot',
'pivot_table',
'plot',
'pop',
'pow',
'prod',
'product',
'quantile',
'query',
'radd',
'rank',
'rdiv',
'reindex',
'reindex_axis',
'reindex_like',
'rename',
'rename_axis',
'reorder_levels',
'replace',
'resample',
'reset_index',
'rfloordiv',
'rmod',
'rmul',
'rolling',
'round',
'rpow',
'rsub',
'rtruediv',
'sample',
'select',
'select_dtypes',
'sem',
'set_axis',
'set_index',
'shape',
'shift',
'size',
'skew',
'slice_shift',
'sort_index',
'sort_values',
'squeeze',
'stack',
'std',
'style',
'sub',
'subtract',
'sum',
'swapaxes',
'swaplevel',
'tail',
'take',
'to_clipboard',
'to_csv',
'to_dense',
'to_dict',
'to_excel',
'to_feather',
'to_gbq',
'to_hdf',
'to_html',
'to_json',
'to_latex',
'to_msgpack',
'to_panel',
'to_parquet',
'to_period',
'to_pickle',
'to_records',
'to_sparse',
'to_sql',
'to_stata',
'to_string',
'to_timestamp',
'to_xarray',
'transform',
'transpose',
'truediv',
'truncate',
'tshift',
'tz_convert',
'tz_localize',
'unstack',
'update',
'values',
'var',
'where',
'xs']
In [19]:
help(europe.sort_values)
Help on method sort_values in module pandas.core.frame:
sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last') method of pandas.core.frame.DataFrame instance
Sort by the values along either axis
Parameters
----------
by : str or list of str
Name or list of names to sort by.
- if `axis` is 0 or `'index'` then `by` may contain index
levels and/or column labels
- if `axis` is 1 or `'columns'` then `by` may contain column
levels and/or index labels
.. versionchanged:: 0.23.0
Allow specifying index or column level names.
axis : {0 or 'index', 1 or 'columns'}, default 0
Axis to be sorted
ascending : bool or list of bool, default True
Sort ascending vs. descending. Specify list for multiple sort
orders. If this is a list of bools, must match the length of
the by.
inplace : bool, default False
if True, perform operation in-place
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
Choice of sorting algorithm. See also ndarray.np.sort for more
information. `mergesort` is the only stable algorithm. For
DataFrames, this option is only applied when sorting on a single
column or label.
na_position : {'first', 'last'}, default 'last'
`first` puts NaNs at the beginning, `last` puts NaNs at the end
Returns
-------
sorted_obj : DataFrame
Examples
--------
>>> df = pd.DataFrame({
... 'col1' : ['A', 'A', 'B', np.nan, 'D', 'C'],
... 'col2' : [2, 1, 9, 8, 7, 4],
... 'col3': [0, 1, 9, 4, 2, 3],
... })
>>> df
col1 col2 col3
0 A 2 0
1 A 1 1
2 B 9 9
3 NaN 8 4
4 D 7 2
5 C 4 3
Sort by col1
>>> df.sort_values(by=['col1'])
col1 col2 col3
0 A 2 0
1 A 1 1
2 B 9 9
5 C 4 3
4 D 7 2
3 NaN 8 4
Sort by multiple columns
>>> df.sort_values(by=['col1', 'col2'])
col1 col2 col3
1 A 1 1
0 A 2 0
2 B 9 9
5 C 4 3
4 D 7 2
3 NaN 8 4
Sort Descending
>>> df.sort_values(by='col1', ascending=False)
col1 col2 col3
4 D 7 2
5 C 4 3
2 B 9 9
0 A 2 0
1 A 1 1
3 NaN 8 4
Putting NAs first
>>> df.sort_values(by='col1', ascending=False, na_position='first')
col1 col2 col3
3 NaN 8 4
4 D 7 2
5 C 4 3
2 B 9 9
0 A 2 0
1 A 1 1
In [20]:
europe.sort_values('gdpPercap_2007', ascending=False)
Out[20]:
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
gdpPercap_2002
gdpPercap_2007
country
Norway
10095.421720
11653.973040
13450.401510
16361.876470
18965.055510
23311.349390
26298.635310
31540.974800
33965.661150
41283.164330
44683.975250
49357.190170
Ireland
5210.280328
5599.077872
6631.597314
7655.568963
9530.772896
11150.981130
12618.321410
13872.866520
17558.815550
24521.947130
34077.049390
40675.996350
Switzerland
14734.232750
17909.489730
20431.092700
22966.144320
27195.113040
26982.290520
28397.715120
30281.704590
31871.530300
32135.323010
34480.957710
37506.419070
Netherlands
8941.571858
11276.193440
12790.849560
15363.251360
18794.745670
21209.059200
21399.460460
23651.323610
26790.949610
30246.130630
33724.757780
36797.933320
Iceland
7267.688428
9244.001412
10350.159060
13319.895680
15798.063620
19654.962470
23269.607500
26923.206280
25144.392010
28061.099660
31163.201960
36180.789190
Austria
6137.076492
8842.598030
10750.721110
12834.602400
16661.625600
19749.422300
21597.083620
23687.826070
27042.018680
29095.920660
32417.607690
36126.492700
Denmark
9692.385245
11099.659350
13583.313510
15937.211230
18866.207210
20422.901500
21688.040480
25116.175810
26406.739850
29804.345670
32166.500060
35278.418740
Sweden
8527.844662
9911.878226
12329.441920
15258.296970
17832.024640
18855.725210
20667.381250
23586.929270
23880.016830
25266.594990
29341.630930
33859.748350
Belgium
8343.105127
9714.960623
10991.206760
13149.041190
16672.143560
19117.974480
20979.845890
22525.563080
25575.570690
27561.196630
30485.883750
33692.605080
Finland
6424.519071
7545.415386
9371.842561
10921.636260
14358.875900
15605.422830
18533.157610
21141.012230
20647.164990
23723.950200
28204.590570
33207.084400
United Kingdom
9979.508487
11283.177950
12477.177070
14142.850890
15895.116410
17428.748460
18232.424520
21664.787670
22705.092540
26074.531360
29478.999190
33203.261280
Germany
7144.114393
10187.826650
12902.462910
14745.625610
18016.180270
20512.921230
22031.532740
24639.185660
26505.303170
27788.884160
30035.801980
32170.374420
France
7029.809327
8662.834898
10560.485530
12999.917660
16107.191710
18292.635140
20293.897460
22066.442140
24703.796150
25889.784870
28926.032340
30470.016700
Spain
3834.034742
4564.802410
5693.843879
7993.512294
10638.751310
13236.921170
13926.169970
15764.983130
18603.064520
20445.298960
24835.471660
28821.063700
Italy
4931.404155
6248.656232
8243.582340
10022.401310
12269.273780
14255.984750
16537.483500
19207.234820
22013.644860
24675.024460
27968.098170
28569.719700
Greece
3530.690067
4916.299889
6017.190733
8513.097016
12724.829570
14195.524280
15268.420890
16120.528390
17541.496340
18747.698140
22514.254800
27538.411880
Slovenia
4215.041741
5862.276629
7402.303395
9405.489397
12383.486200
15277.030170
17866.721750
18678.534920
14214.716810
17161.107350
20660.019360
25768.257590
Czech Republic
6876.140250
8256.343918
10136.867130
11399.444890
13108.453600
14800.160620
15377.228550
16310.443400
14297.021220
16048.514240
17596.210220
22833.308510
Portugal
3068.319867
3774.571743
4727.954889
6361.517993
9022.247417
10172.485720
11753.842910
13039.308760
16207.266630
17641.031560
19970.907870
20509.647770
Slovak Republic
5074.659104
6093.262980
7481.107598
8412.902397
9674.167626
10922.664040
11348.545850
12037.267580
9498.467723
12126.230650
13638.778370
18678.314350
Hungary
5263.673816
6040.180011
7550.359877
9326.644670
10168.656110
11674.837370
12545.990660
12986.479980
10535.628550
11712.776800
14843.935560
18008.944440
Poland
4029.329699
4734.253019
5338.752143
6557.152776
8006.506993
9508.141454
8451.531004
9082.351172
7738.881247
10159.583680
12002.239080
15389.924680
Croatia
3119.236520
4338.231617
5477.890018
6960.297861
9164.090127
11305.385170
13221.821840
13822.583940
8447.794873
9875.604515
11628.388950
14619.222720
Romania
3144.613186
3943.370225
4734.997586
6470.866545
8011.414402
9356.397240
9605.314053
9696.273295
6598.409903
7346.547557
7885.360081
10808.475610
Bulgaria
2444.286648
3008.670727
4254.337839
5577.002800
6597.494398
7612.240438
8224.191647
8239.854824
6302.623438
5970.388760
7696.777725
10680.792820
Serbia
3581.459448
4981.090891
6289.629157
7991.707066
10522.067490
12980.669560
15181.092700
15870.878510
9325.068238
7914.320304
7236.075251
9786.534714
Montenegro
2647.585601
3682.259903
4649.593785
5907.850937
7778.414017
9595.929905
11222.587620
11732.510170
7003.339037
6465.613349
6557.194282
9253.896111
Turkey
1969.100980
2218.754257
2322.869908
2826.356387
3450.696380
4269.122326
4241.356344
5089.043686
5678.348271
6601.429915
6508.085718
8458.276384
Bosnia and Herzegovina
973.533195
1353.989176
1709.683679
2172.352423
2860.169750
3528.481305
4126.613157
4314.114757
2546.781445
4766.355904
6018.975239
7446.298803
Albania
1601.056136
1942.284244
2312.888958
2760.196931
3313.422188
3533.003910
3630.880722
3738.932735
2497.437901
3193.054604
4604.211737
5937.029526
In [21]:
europe['gdpPercap_2007']
Out[21]:
country
Albania 5937.029526
Austria 36126.492700
Belgium 33692.605080
Bosnia and Herzegovina 7446.298803
Bulgaria 10680.792820
Croatia 14619.222720
Czech Republic 22833.308510
Denmark 35278.418740
Finland 33207.084400
France 30470.016700
Germany 32170.374420
Greece 27538.411880
Hungary 18008.944440
Iceland 36180.789190
Ireland 40675.996350
Italy 28569.719700
Montenegro 9253.896111
Netherlands 36797.933320
Norway 49357.190170
Poland 15389.924680
Portugal 20509.647770
Romania 10808.475610
Serbia 9786.534714
Slovak Republic 18678.314350
Slovenia 25768.257590
Spain 28821.063700
Sweden 33859.748350
Switzerland 37506.419070
Turkey 8458.276384
United Kingdom 33203.261280
Name: gdpPercap_2007, dtype: float64
In [22]:
europe.gdpPercap_2007
Out[22]:
country
Albania 5937.029526
Austria 36126.492700
Belgium 33692.605080
Bosnia and Herzegovina 7446.298803
Bulgaria 10680.792820
Croatia 14619.222720
Czech Republic 22833.308510
Denmark 35278.418740
Finland 33207.084400
France 30470.016700
Germany 32170.374420
Greece 27538.411880
Hungary 18008.944440
Iceland 36180.789190
Ireland 40675.996350
Italy 28569.719700
Montenegro 9253.896111
Netherlands 36797.933320
Norway 49357.190170
Poland 15389.924680
Portugal 20509.647770
Romania 10808.475610
Serbia 9786.534714
Slovak Republic 18678.314350
Slovenia 25768.257590
Spain 28821.063700
Sweden 33859.748350
Switzerland 37506.419070
Turkey 8458.276384
United Kingdom 33203.261280
Name: gdpPercap_2007, dtype: float64
In [26]:
# loc include last index value
europe.loc['Belgium':'Bulgaria',:]
Out[26]:
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
gdpPercap_2002
gdpPercap_2007
country
Belgium
8343.105127
9714.960623
10991.206760
13149.041190
16672.143560
19117.974480
20979.845890
22525.563080
25575.570690
27561.196630
30485.883750
33692.605080
Bosnia and Herzegovina
973.533195
1353.989176
1709.683679
2172.352423
2860.169750
3528.481305
4126.613157
4314.114757
2546.781445
4766.355904
6018.975239
7446.298803
Bulgaria
2444.286648
3008.670727
4254.337839
5577.002800
6597.494398
7612.240438
8224.191647
8239.854824
6302.623438
5970.388760
7696.777725
10680.792820
In [27]:
europe.iloc[2:4,:]
Out[27]:
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
gdpPercap_2002
gdpPercap_2007
country
Belgium
8343.105127
9714.960623
10991.206760
13149.041190
16672.14356
19117.974480
20979.845890
22525.563080
25575.570690
27561.196630
30485.883750
33692.605080
Bosnia and Herzegovina
973.533195
1353.989176
1709.683679
2172.352423
2860.16975
3528.481305
4126.613157
4314.114757
2546.781445
4766.355904
6018.975239
7446.298803
In [29]:
europe.max()
Out[29]:
gdpPercap_1952 14734.23275
gdpPercap_1957 17909.48973
gdpPercap_1962 20431.09270
gdpPercap_1967 22966.14432
gdpPercap_1972 27195.11304
gdpPercap_1977 26982.29052
gdpPercap_1982 28397.71512
gdpPercap_1987 31540.97480
gdpPercap_1992 33965.66115
gdpPercap_1997 41283.16433
gdpPercap_2002 44683.97525
gdpPercap_2007 49357.19017
dtype: float64
In [30]:
europe.loc['Italy':'Poland', 'gdpPercap_1962':'gdpPercap_1972']
Out[30]:
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
country
Italy
8243.582340
10022.401310
12269.273780
Montenegro
4649.593785
5907.850937
7778.414017
Netherlands
12790.849560
15363.251360
18794.745670
Norway
13450.401510
16361.876470
18965.055510
Poland
5338.752143
6557.152776
8006.506993
In [28]:
europe.loc['Italy':'Poland', 'gdpPercap_1962':'gdpPercap_1972'].max()
Out[28]:
gdpPercap_1962 13450.40151
gdpPercap_1967 16361.87647
gdpPercap_1972 18965.05551
dtype: float64
In [31]:
europe.loc['Italy':'Poland', 'gdpPercap_1962':'gdpPercap_1972'].min()
Out[31]:
gdpPercap_1962 4649.593785
gdpPercap_1967 5907.850937
gdpPercap_1972 7778.414017
dtype: float64
In [32]:
# subset on condition
subset = europe.loc['Italy':'Poland', 'gdpPercap_1962':'gdpPercap_1972']
print('Subset:\n', subset)
Subset:
gdpPercap_1962 gdpPercap_1967 gdpPercap_1972
country
Italy 8243.582340 10022.401310 12269.273780
Montenegro 4649.593785 5907.850937 7778.414017
Netherlands 12790.849560 15363.251360 18794.745670
Norway 13450.401510 16361.876470 18965.055510
Poland 5338.752143 6557.152776 8006.506993
In [33]:
print('Values greater than 10.000', subset > 10000)
Values greater than 10.000 gdpPercap_1962 gdpPercap_1967 gdpPercap_1972
country
Italy False True True
Montenegro False False False
Netherlands True True True
Norway True True True
Poland False False False
In [35]:
mask = subset > 10000
print(subset[mask])
gdpPercap_1962 gdpPercap_1967 gdpPercap_1972
country
Italy NaN 10022.40131 12269.27378
Montenegro NaN NaN NaN
Netherlands 12790.84956 15363.25136 18794.74567
Norway 13450.40151 16361.87647 18965.05551
Poland NaN NaN NaN
In [36]:
print(subset[mask].describe())
gdpPercap_1962 gdpPercap_1967 gdpPercap_1972
count 2.000000 3.000000 3.000000
mean 13120.625535 13915.843047 16676.358320
std 466.373656 3408.589070 3817.597015
min 12790.849560 10022.401310 12269.273780
25% 12955.737547 12692.826335 15532.009725
50% 13120.625535 15363.251360 18794.745670
75% 13285.513523 15862.563915 18879.900590
max 13450.401510 16361.876470 18965.055510
In [6]:
europe['gdpPercap_2007']['Serbia']
Out[6]:
9786.534714
In [7]:
europe.loc['Serbia', 'gdpPercap_2007']
Out[7]:
9786.534714
In [9]:
spain = europe.loc['Spain']
In [18]:
spain
Out[18]:
gdpPercap_1952 3834.034742
gdpPercap_1957 4564.802410
gdpPercap_1962 5693.843879
gdpPercap_1967 7993.512294
gdpPercap_1972 10638.751310
gdpPercap_1977 13236.921170
gdpPercap_1982 13926.169970
gdpPercap_1987 15764.983130
gdpPercap_1992 18603.064520
gdpPercap_1997 20445.298960
gdpPercap_2002 24835.471660
gdpPercap_2007 28821.063700
Name: Spain, dtype: float64
In [10]:
import matplotlib.pyplot as mp
In [13]:
help(mp.plot)
Help on function plot in module matplotlib.pyplot:
plot(*args, **kwargs)
Plot y versus x as lines and/or markers.
Call signatures::
plot([x], y, [fmt], data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
The coordinates of the points or line nodes are given by *x*, *y*.
The optional parameter *fmt* is a convenient way for defining basic
formatting like color, marker and linestyle. It's a shortcut string
notation described in the *Notes* section below.
>>> plot(x, y) # plot x and y using default line style and color
>>> plot(x, y, 'bo') # plot x and y using blue circle markers
>>> plot(y) # plot y using x as index array 0..N-1
>>> plot(y, 'r+') # ditto, but with red plusses
You can use `.Line2D` properties as keyword arguments for more
control on the appearance. Line properties and *fmt* can be mixed.
The following two calls yield identical results:
>>> plot(x, y, 'go--', linewidth=2, markersize=12)
>>> plot(x, y, color='green', marker='o', linestyle='dashed',
linewidth=2, markersize=12)
When conflicting with *fmt*, keyword arguments take precedence.
**Plotting labelled data**
There's a convenient way for plotting objects with labelled data (i.e.
data that can be accessed by index ``obj['y']``). Instead of giving
the data in *x* and *y*, you can provide the object in the *data*
parameter and just give the labels for *x* and *y*::
>>> plot('xlabel', 'ylabel', data=obj)
All indexable objects are supported. This could e.g. be a `dict`, a
`pandas.DataFame` or a structured numpy array.
**Plotting multiple sets of data**
There are various ways to plot multiple sets of data.
- The most straight forward way is just to call `plot` multiple times.
Example:
>>> plot(x1, y1, 'bo')
>>> plot(x2, y2, 'go')
- Alternatively, if your data is already a 2d array, you can pass it
directly to *x*, *y*. A separate data set will be drawn for every
column.
Example: an array ``a`` where the first column represents the *x*
values and the other columns are the *y* columns::
>>> plot(a[0], a[1:])
- The third way is to specify multiple sets of *[x]*, *y*, *[fmt]*
groups::
>>> plot(x1, y1, 'g^', x2, y2, 'g-')
In this case, any additional keyword argument applies to all
datasets. Also this syntax cannot be combined with the *data*
parameter.
By default, each line is assigned a different style specified by a
'style cycle'. The *fmt* and line property parameters are only
necessary if you want explicit deviations from these defaults.
Alternatively, you can also change the style cycle using the
'axes.prop_cycle' rcParam.
Parameters
----------
x, y : array-like or scalar
The horizontal / vertical coordinates of the data points.
*x* values are optional. If not given, they default to
``[0, ..., N-1]``.
Commonly, these parameters are arrays of length N. However,
scalars are supported as well (equivalent to an array with
constant value).
The parameters can also be 2-dimensional. Then, the columns
represent separate data sets.
fmt : str, optional
A format string, e.g. 'ro' for red circles. See the *Notes*
section for a full description of the format strings.
Format strings are just an abbreviation for quickly setting
basic line properties. All of these and more can also be
controlled by keyword arguments.
data : indexable object, optional
An object with labelled data. If given, provide the label names to
plot in *x* and *y*.
.. note::
Technically there's a slight ambiguity in calls where the
second label is a valid *fmt*. `plot('n', 'o', data=obj)`
could be `plt(x, y)` or `plt(y, fmt)`. In such cases,
the former interpretation is chosen, but a warning is issued.
You may suppress the warning by adding an empty format string
`plot('n', 'o', '', data=obj)`.
Other Parameters
----------------
scalex, scaley : bool, optional, default: True
These parameters determined if the view limits are adapted to
the data limits. The values are passed on to `autoscale_view`.
**kwargs : `.Line2D` properties, optional
*kwargs* are used to specify properties like a line label (for
auto legends), linewidth, antialiasing, marker face color.
Example::
>>> plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
>>> plot([1,2,3], [1,4,9], 'rs', label='line 2')
If you make multiple lines with one plot command, the kwargs
apply to all those lines.
Here is a list of available `.Line2D` properties:
agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alpha: float (0.0 transparent through 1.0 opaque)
animated: bool
antialiased or aa: bool
clip_box: a `.Bbox` instance
clip_on: bool
clip_path: [(`~matplotlib.path.Path`, `.Transform`) | `.Patch` | None]
color or c: any matplotlib color
contains: a callable function
dash_capstyle: ['butt' | 'round' | 'projecting']
dash_joinstyle: ['miter' | 'round' | 'bevel']
dashes: sequence of on/off ink in points
drawstyle: ['default' | 'steps' | 'steps-pre' | 'steps-mid' | 'steps-post']
figure: a `.Figure` instance
fillstyle: ['full' | 'left' | 'right' | 'bottom' | 'top' | 'none']
gid: an id string
label: object
linestyle or ls: ['solid' | 'dashed', 'dashdot', 'dotted' | (offset, on-off-dash-seq) | ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` | ``' '`` | ``''``]
linewidth or lw: float value in points
marker: :mod:`A valid marker style <matplotlib.markers>`
markeredgecolor or mec: any matplotlib color
markeredgewidth or mew: float value in points
markerfacecolor or mfc: any matplotlib color
markerfacecoloralt or mfcalt: any matplotlib color
markersize or ms: float
markevery: [None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float]
path_effects: `.AbstractPathEffect`
picker: float distance in points or callable pick function ``fn(artist, event)``
pickradius: float distance in points
rasterized: bool or None
sketch_params: (scale: float, length: float, randomness: float)
snap: bool or None
solid_capstyle: ['butt' | 'round' | 'projecting']
solid_joinstyle: ['miter' | 'round' | 'bevel']
transform: a :class:`matplotlib.transforms.Transform` instance
url: a url string
visible: bool
xdata: 1D array
ydata: 1D array
zorder: float
Returns
-------
lines
A list of `.Line2D` objects representing the plotted data.
See Also
--------
scatter : XY scatter plot with markers of variing size and/or color (
sometimes also called bubble chart).
Notes
-----
**Format Strings**
A format string consists of a part for color, marker and line::
fmt = '[color][marker][line]'
Each of them is optional. If not provided, the value from the style
cycle is used. Exception: If ``line`` is given, but no ``marker``,
the data will be a line without markers.
**Colors**
The following color abbreviations are supported:
============= ===============================
character color
============= ===============================
``'b'`` blue
``'g'`` green
``'r'`` red
``'c'`` cyan
``'m'`` magenta
``'y'`` yellow
``'k'`` black
``'w'`` white
============= ===============================
If the color is the only part of the format string, you can
additionally use any `matplotlib.colors` spec, e.g. full names
(``'green'``) or hex strings (``'#008000'``).
**Markers**
============= ===============================
character description
============= ===============================
``'.'`` point marker
``','`` pixel marker
``'o'`` circle marker
``'v'`` triangle_down marker
``'^'`` triangle_up marker
``'<'`` triangle_left marker
``'>'`` triangle_right marker
``'1'`` tri_down marker
``'2'`` tri_up marker
``'3'`` tri_left marker
``'4'`` tri_right marker
``'s'`` square marker
``'p'`` pentagon marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x marker
``'D'`` diamond marker
``'d'`` thin_diamond marker
``'|'`` vline marker
``'_'`` hline marker
============= ===============================
**Line Styles**
============= ===============================
character description
============= ===============================
``'-'`` solid line style
``'--'`` dashed line style
``'-.'`` dash-dot line style
``':'`` dotted line style
============= ===============================
Example format strings::
'b' # blue markers with default shape
'ro' # red circles
'g-' # green solid line
'--' # dashed line with default color
'k^:' # black triangle_up markers connected by a dotted line
.. note::
In addition to the above described arguments, this function can take a
**data** keyword argument. If such a **data** argument is given, the
following arguments are replaced by **data[<arg>]**:
* All arguments with the following names: 'x', 'y'.
In [15]:
spain.values
Out[15]:
array([ 3834.034742, 4564.80241 , 5693.843879, 7993.512294,
10638.75131 , 13236.92117 , 13926.16997 , 15764.98313 ,
18603.06452 , 20445.29896 , 24835.47166 , 28821.0637 ])
In [14]:
spain.index
Out[14]:
Index(['gdpPercap_1952', 'gdpPercap_1957', 'gdpPercap_1962', 'gdpPercap_1967',
'gdpPercap_1972', 'gdpPercap_1977', 'gdpPercap_1982', 'gdpPercap_1987',
'gdpPercap_1992', 'gdpPercap_1997', 'gdpPercap_2002', 'gdpPercap_2007'],
dtype='object')
In [53]:
years = [int(x.replace('gdpPercap_', '')) for x in spain.index]
In [54]:
years
Out[54]:
[1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007]
In [55]:
mp.plot(spain_years, spain.values)
Out[55]:
[<matplotlib.lines.Line2D at 0x117590ba8>]
In [57]:
mp.plot(years, europe.loc['Germany'].values)
Out[57]:
[<matplotlib.lines.Line2D at 0x1176499b0>]
In [34]:
print(europe.iloc[0:2, 0:2])
gdpPercap_1952 gdpPercap_1957
country
Albania 1601.056136 1942.284244
Austria 6137.076492 8842.598030
In [35]:
print(europe.loc['Albania':'Belgium', 'gdpPercap_1952':'gdpPercap_1962'])
gdpPercap_1952 gdpPercap_1957 gdpPercap_1962
country
Albania 1601.056136 1942.284244 2312.888958
Austria 6137.076492 8842.598030 10750.721110
Belgium 8343.105127 9714.960623 10991.206760
In [42]:
first = pd.read_csv('data/gapminder_all.csv', index_col='country')
In [43]:
second = first[first['continent'] == 'Americas']
In [44]:
second
Out[44]:
continent
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
...
pop_1962
pop_1967
pop_1972
pop_1977
pop_1982
pop_1987
pop_1992
pop_1997
pop_2002
pop_2007
country
Argentina
Americas
5911.315053
6856.856212
7133.166023
8052.953021
9443.038526
10079.026740
8997.897412
9139.671389
9308.418710
...
21283783.0
22934225.0
24779799.0
26983828.0
29341374.0
31620918.0
33958947.0
36203463.0
38331121
40301927
Bolivia
Americas
2677.326347
2127.686326
2180.972546
2586.886053
2980.331339
3548.097832
3156.510452
2753.691490
2961.699694
...
3593918.0
4040665.0
4565872.0
5079716.0
5642224.0
6156369.0
6893451.0
7693188.0
8445134
9119152
Brazil
Americas
2108.944355
2487.365989
3336.585802
3429.864357
4985.711467
6660.118654
7030.835878
7807.095818
6950.283021
...
76039390.0
88049823.0
100840058.0
114313951.0
128962939.0
142938076.0
155975974.0
168546719.0
179914212
190010647
Canada
Americas
11367.161120
12489.950060
13462.485550
16076.588030
18970.570860
22090.883060
22898.792140
26626.515030
26342.884260
...
18985849.0
20819767.0
22284500.0
23796400.0
25201900.0
26549700.0
28523502.0
30305843.0
31902268
33390141
Chile
Americas
3939.978789
4315.622723
4519.094331
5106.654313
5494.024437
4756.763836
5095.665738
5547.063754
7596.125964
...
7961258.0
8858908.0
9717524.0
10599793.0
11487112.0
12463354.0
13572994.0
14599929.0
15497046
16284741
Colombia
Americas
2144.115096
2323.805581
2492.351109
2678.729839
3264.660041
3815.807870
4397.575659
4903.219100
5444.648617
...
17009885.0
19764027.0
22542890.0
25094412.0
27764644.0
30964245.0
34202721.0
37657830.0
41008227
44227550
Costa Rica
Americas
2627.009471
2990.010802
3460.937025
4161.727834
5118.146939
5926.876967
5262.734751
5629.915318
6160.416317
...
1345187.0
1588717.0
1834796.0
2108457.0
2424367.0
2799811.0
3173216.0
3518107.0
3834934
4133884
Cuba
Americas
5586.538780
6092.174359
5180.755910
5690.268015
5305.445256
6380.494966
7316.918107
7532.924763
5592.843963
...
7254373.0
8139332.0
8831348.0
9537988.0
9789224.0
10239839.0
10723260.0
10983007.0
11226999
11416987
Dominican Republic
Americas
1397.717137
1544.402995
1662.137359
1653.723003
2189.874499
2681.988900
2861.092386
2899.842175
3044.214214
...
3453434.0
4049146.0
4671329.0
5302800.0
5968349.0
6655297.0
7351181.0
7992357.0
8650322
9319622
Ecuador
Americas
3522.110717
3780.546651
4086.114078
4579.074215
5280.994710
6679.623260
7213.791267
6481.776993
7103.702595
...
4681707.0
5432424.0
6298651.0
7278866.0
8365850.0
9545158.0
10748394.0
11911819.0
12921234
13755680
El Salvador
Americas
3048.302900
3421.523218
3776.803627
4358.595393
4520.246008
5138.922374
4098.344175
4140.442097
4444.231700
...
2747687.0
3232927.0
3790903.0
4282586.0
4474873.0
4842194.0
5274649.0
5783439.0
6353681
6939688
Guatemala
Americas
2428.237769
2617.155967
2750.364446
3242.531147
4031.408271
4879.992748
4820.494790
4246.485974
4439.450840
...
4208858.0
4690773.0
5149581.0
5703430.0
6395630.0
7326406.0
8486949.0
9803875.0
11178650
12572928
Haiti
Americas
1840.366939
1726.887882
1796.589032
1452.057666
1654.456946
1874.298931
2011.159549
1823.015995
1456.309517
...
3880130.0
4318137.0
4698301.0
4908554.0
5198399.0
5756203.0
6326682.0
6913545.0
7607651
8502814
Honduras
Americas
2194.926204
2220.487682
2291.156835
2538.269358
2529.842345
3203.208066
3121.760794
3023.096699
3081.694603
...
2090162.0
2500689.0
2965146.0
3055235.0
3669448.0
4372203.0
5077347.0
5867957.0
6677328
7483763
Jamaica
Americas
2898.530881
4756.525781
5246.107524
6124.703451
7433.889293
6650.195573
6068.051350
6351.237495
7404.923685
...
1665128.0
1861096.0
1997616.0
2156814.0
2298309.0
2326606.0
2378618.0
2531311.0
2664659
2780132
Mexico
Americas
3478.125529
4131.546641
4581.609385
5754.733883
6809.406690
7674.929108
9611.147541
8688.156003
9472.384295
...
41121485.0
47995559.0
55984294.0
63759976.0
71640904.0
80122492.0
88111030.0
95895146.0
102479927
108700891
Nicaragua
Americas
3112.363948
3457.415947
3634.364406
4643.393534
4688.593267
5486.371089
3470.338156
2955.984375
2170.151724
...
1590597.0
1865490.0
2182908.0
2554598.0
2979423.0
3344353.0
4017939.0
4609572.0
5146848
5675356
Panama
Americas
2480.380334
2961.800905
3536.540301
4421.009084
5364.249663
5351.912144
7009.601598
7034.779161
6618.743050
...
1215725.0
1405486.0
1616384.0
1839782.0
2036305.0
2253639.0
2484997.0
2734531.0
2990875
3242173
Paraguay
Americas
1952.308701
2046.154706
2148.027146
2299.376311
2523.337977
3248.373311
4258.503604
3998.875695
4196.411078
...
2009813.0
2287985.0
2614104.0
2984494.0
3366439.0
3886512.0
4483945.0
5154123.0
5884491
6667147
Peru
Americas
3758.523437
4245.256698
4957.037982
5788.093330
5937.827283
6281.290855
6434.501797
6360.943444
4446.380924
...
10516500.0
12132200.0
13954700.0
15990099.0
18125129.0
20195924.0
22430449.0
24748122.0
26769436
28674757
Puerto Rico
Americas
3081.959785
3907.156189
5108.344630
6929.277714
9123.041742
9770.524921
10330.989150
12281.341910
14641.587110
...
2448046.0
2648961.0
2847132.0
3080828.0
3279001.0
3444468.0
3585176.0
3759430.0
3859606
3942491
Trinidad and Tobago
Americas
3023.271928
4100.393400
4997.523971
5621.368472
6619.551419
7899.554209
9119.528607
7388.597823
7370.990932
...
887498.0
960155.0
975199.0
1039009.0
1116479.0
1191336.0
1183669.0
1138101.0
1101832
1056608
United States
Americas
13990.482080
14847.127120
16173.145860
19530.365570
21806.035940
24072.632130
25009.559140
29884.350410
32003.932240
...
186538000.0
198712000.0
209896000.0
220239000.0
232187835.0
242803533.0
256894189.0
272911760.0
287675526
301139947
Uruguay
Americas
5716.766744
6150.772969
5603.357717
5444.619620
5703.408898
6504.339663
6920.223051
7452.398969
8137.004775
...
2598466.0
2748579.0
2829526.0
2873520.0
2953997.0
3045153.0
3149262.0
3262838.0
3363085
3447496
Venezuela
Americas
7689.799761
9802.466526
8422.974165
9541.474188
10505.259660
13143.950950
11152.410110
9883.584648
10733.926310
...
8143375.0
9709552.0
11515649.0
13503563.0
15620766.0
17910182.0
20265563.0
22374398.0
24287670
26084662
25 rows × 37 columns
In [45]:
third = second.drop('Puerto Rico')
In [46]:
fourth = third.drop('continent', axis=1)
In [47]:
fourth
Out[47]:
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
...
pop_1962
pop_1967
pop_1972
pop_1977
pop_1982
pop_1987
pop_1992
pop_1997
pop_2002
pop_2007
country
Argentina
5911.315053
6856.856212
7133.166023
8052.953021
9443.038526
10079.026740
8997.897412
9139.671389
9308.418710
10967.281950
...
21283783.0
22934225.0
24779799.0
26983828.0
29341374.0
31620918.0
33958947.0
36203463.0
38331121
40301927
Bolivia
2677.326347
2127.686326
2180.972546
2586.886053
2980.331339
3548.097832
3156.510452
2753.691490
2961.699694
3326.143191
...
3593918.0
4040665.0
4565872.0
5079716.0
5642224.0
6156369.0
6893451.0
7693188.0
8445134
9119152
Brazil
2108.944355
2487.365989
3336.585802
3429.864357
4985.711467
6660.118654
7030.835878
7807.095818
6950.283021
7957.980824
...
76039390.0
88049823.0
100840058.0
114313951.0
128962939.0
142938076.0
155975974.0
168546719.0
179914212
190010647
Canada
11367.161120
12489.950060
13462.485550
16076.588030
18970.570860
22090.883060
22898.792140
26626.515030
26342.884260
28954.925890
...
18985849.0
20819767.0
22284500.0
23796400.0
25201900.0
26549700.0
28523502.0
30305843.0
31902268
33390141
Chile
3939.978789
4315.622723
4519.094331
5106.654313
5494.024437
4756.763836
5095.665738
5547.063754
7596.125964
10118.053180
...
7961258.0
8858908.0
9717524.0
10599793.0
11487112.0
12463354.0
13572994.0
14599929.0
15497046
16284741
Colombia
2144.115096
2323.805581
2492.351109
2678.729839
3264.660041
3815.807870
4397.575659
4903.219100
5444.648617
6117.361746
...
17009885.0
19764027.0
22542890.0
25094412.0
27764644.0
30964245.0
34202721.0
37657830.0
41008227
44227550
Costa Rica
2627.009471
2990.010802
3460.937025
4161.727834
5118.146939
5926.876967
5262.734751
5629.915318
6160.416317
6677.045314
...
1345187.0
1588717.0
1834796.0
2108457.0
2424367.0
2799811.0
3173216.0
3518107.0
3834934
4133884
Cuba
5586.538780
6092.174359
5180.755910
5690.268015
5305.445256
6380.494966
7316.918107
7532.924763
5592.843963
5431.990415
...
7254373.0
8139332.0
8831348.0
9537988.0
9789224.0
10239839.0
10723260.0
10983007.0
11226999
11416987
Dominican Republic
1397.717137
1544.402995
1662.137359
1653.723003
2189.874499
2681.988900
2861.092386
2899.842175
3044.214214
3614.101285
...
3453434.0
4049146.0
4671329.0
5302800.0
5968349.0
6655297.0
7351181.0
7992357.0
8650322
9319622
Ecuador
3522.110717
3780.546651
4086.114078
4579.074215
5280.994710
6679.623260
7213.791267
6481.776993
7103.702595
7429.455877
...
4681707.0
5432424.0
6298651.0
7278866.0
8365850.0
9545158.0
10748394.0
11911819.0
12921234
13755680
El Salvador
3048.302900
3421.523218
3776.803627
4358.595393
4520.246008
5138.922374
4098.344175
4140.442097
4444.231700
5154.825496
...
2747687.0
3232927.0
3790903.0
4282586.0
4474873.0
4842194.0
5274649.0
5783439.0
6353681
6939688
Guatemala
2428.237769
2617.155967
2750.364446
3242.531147
4031.408271
4879.992748
4820.494790
4246.485974
4439.450840
4684.313807
...
4208858.0
4690773.0
5149581.0
5703430.0
6395630.0
7326406.0
8486949.0
9803875.0
11178650
12572928
Haiti
1840.366939
1726.887882
1796.589032
1452.057666
1654.456946
1874.298931
2011.159549
1823.015995
1456.309517
1341.726931
...
3880130.0
4318137.0
4698301.0
4908554.0
5198399.0
5756203.0
6326682.0
6913545.0
7607651
8502814
Honduras
2194.926204
2220.487682
2291.156835
2538.269358
2529.842345
3203.208066
3121.760794
3023.096699
3081.694603
3160.454906
...
2090162.0
2500689.0
2965146.0
3055235.0
3669448.0
4372203.0
5077347.0
5867957.0
6677328
7483763
Jamaica
2898.530881
4756.525781
5246.107524
6124.703451
7433.889293
6650.195573
6068.051350
6351.237495
7404.923685
7121.924704
...
1665128.0
1861096.0
1997616.0
2156814.0
2298309.0
2326606.0
2378618.0
2531311.0
2664659
2780132
Mexico
3478.125529
4131.546641
4581.609385
5754.733883
6809.406690
7674.929108
9611.147541
8688.156003
9472.384295
9767.297530
...
41121485.0
47995559.0
55984294.0
63759976.0
71640904.0
80122492.0
88111030.0
95895146.0
102479927
108700891
Nicaragua
3112.363948
3457.415947
3634.364406
4643.393534
4688.593267
5486.371089
3470.338156
2955.984375
2170.151724
2253.023004
...
1590597.0
1865490.0
2182908.0
2554598.0
2979423.0
3344353.0
4017939.0
4609572.0
5146848
5675356
Panama
2480.380334
2961.800905
3536.540301
4421.009084
5364.249663
5351.912144
7009.601598
7034.779161
6618.743050
7113.692252
...
1215725.0
1405486.0
1616384.0
1839782.0
2036305.0
2253639.0
2484997.0
2734531.0
2990875
3242173
Paraguay
1952.308701
2046.154706
2148.027146
2299.376311
2523.337977
3248.373311
4258.503604
3998.875695
4196.411078
4247.400261
...
2009813.0
2287985.0
2614104.0
2984494.0
3366439.0
3886512.0
4483945.0
5154123.0
5884491
6667147
Peru
3758.523437
4245.256698
4957.037982
5788.093330
5937.827283
6281.290855
6434.501797
6360.943444
4446.380924
5838.347657
...
10516500.0
12132200.0
13954700.0
15990099.0
18125129.0
20195924.0
22430449.0
24748122.0
26769436
28674757
Trinidad and Tobago
3023.271928
4100.393400
4997.523971
5621.368472
6619.551419
7899.554209
9119.528607
7388.597823
7370.990932
8792.573126
...
887498.0
960155.0
975199.0
1039009.0
1116479.0
1191336.0
1183669.0
1138101.0
1101832
1056608
United States
13990.482080
14847.127120
16173.145860
19530.365570
21806.035940
24072.632130
25009.559140
29884.350410
32003.932240
35767.433030
...
186538000.0
198712000.0
209896000.0
220239000.0
232187835.0
242803533.0
256894189.0
272911760.0
287675526
301139947
Uruguay
5716.766744
6150.772969
5603.357717
5444.619620
5703.408898
6504.339663
6920.223051
7452.398969
8137.004775
9230.240708
...
2598466.0
2748579.0
2829526.0
2873520.0
2953997.0
3045153.0
3149262.0
3262838.0
3363085
3447496
Venezuela
7689.799761
9802.466526
8422.974165
9541.474188
10505.259660
13143.950950
11152.410110
9883.584648
10733.926310
10165.495180
...
8143375.0
9709552.0
11515649.0
13503563.0
15620766.0
17910182.0
20265563.0
22374398.0
24287670
26084662
24 rows × 36 columns
In [48]:
fourth.to_csv('data/all.csv')
In [49]:
fourth.columns
Out[49]:
Index(['gdpPercap_1952', 'gdpPercap_1957', 'gdpPercap_1962', 'gdpPercap_1967',
'gdpPercap_1972', 'gdpPercap_1977', 'gdpPercap_1982', 'gdpPercap_1987',
'gdpPercap_1992', 'gdpPercap_1997', 'gdpPercap_2002', 'gdpPercap_2007',
'lifeExp_1952', 'lifeExp_1957', 'lifeExp_1962', 'lifeExp_1967',
'lifeExp_1972', 'lifeExp_1977', 'lifeExp_1982', 'lifeExp_1987',
'lifeExp_1992', 'lifeExp_1997', 'lifeExp_2002', 'lifeExp_2007',
'pop_1952', 'pop_1957', 'pop_1962', 'pop_1967', 'pop_1972', 'pop_1977',
'pop_1982', 'pop_1987', 'pop_1992', 'pop_1997', 'pop_2002', 'pop_2007'],
dtype='object')
In [52]:
fourth.loc['Venezuela', 'pop_1952': 'pop_2007']
Out[52]:
pop_1952 5439568.0
pop_1957 6702668.0
pop_1962 8143375.0
pop_1967 9709552.0
pop_1972 11515649.0
pop_1977 13503563.0
pop_1982 15620766.0
pop_1987 17910182.0
pop_1992 20265563.0
pop_1997 22374398.0
pop_2002 24287670.0
pop_2007 26084662.0
Name: Venezuela, dtype: float64
In [58]:
mp.plot(years, fourth.loc['Venezuela', 'pop_1952': 'pop_2007'].values)
Out[58]:
[<matplotlib.lines.Line2D at 0x10580f2b0>]
In [59]:
mp.plot(years, fourth.loc['Venezuela', 'lifeExp_1952': 'lifeExp_2007'].values)
Out[59]:
[<matplotlib.lines.Line2D at 0x1177f4a58>]
In [61]:
mp.plot(years, fourth.loc['Venezuela', 'gdpPercap_1952': 'gdpPercap_2007'].values)
Out[61]:
[<matplotlib.lines.Line2D at 0x117ae4d68>]
In [64]:
europe
Out[64]:
gdpPercap_1952
gdpPercap_1957
gdpPercap_1962
gdpPercap_1967
gdpPercap_1972
gdpPercap_1977
gdpPercap_1982
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
gdpPercap_2002
gdpPercap_2007
country
Albania
1601.056136
1942.284244
2312.888958
2760.196931
3313.422188
3533.003910
3630.880722
3738.932735
2497.437901
3193.054604
4604.211737
5937.029526
Austria
6137.076492
8842.598030
10750.721110
12834.602400
16661.625600
19749.422300
21597.083620
23687.826070
27042.018680
29095.920660
32417.607690
36126.492700
Belgium
8343.105127
9714.960623
10991.206760
13149.041190
16672.143560
19117.974480
20979.845890
22525.563080
25575.570690
27561.196630
30485.883750
33692.605080
Bosnia and Herzegovina
973.533195
1353.989176
1709.683679
2172.352423
2860.169750
3528.481305
4126.613157
4314.114757
2546.781445
4766.355904
6018.975239
7446.298803
Bulgaria
2444.286648
3008.670727
4254.337839
5577.002800
6597.494398
7612.240438
8224.191647
8239.854824
6302.623438
5970.388760
7696.777725
10680.792820
Croatia
3119.236520
4338.231617
5477.890018
6960.297861
9164.090127
11305.385170
13221.821840
13822.583940
8447.794873
9875.604515
11628.388950
14619.222720
Czech Republic
6876.140250
8256.343918
10136.867130
11399.444890
13108.453600
14800.160620
15377.228550
16310.443400
14297.021220
16048.514240
17596.210220
22833.308510
Denmark
9692.385245
11099.659350
13583.313510
15937.211230
18866.207210
20422.901500
21688.040480
25116.175810
26406.739850
29804.345670
32166.500060
35278.418740
Finland
6424.519071
7545.415386
9371.842561
10921.636260
14358.875900
15605.422830
18533.157610
21141.012230
20647.164990
23723.950200
28204.590570
33207.084400
France
7029.809327
8662.834898
10560.485530
12999.917660
16107.191710
18292.635140
20293.897460
22066.442140
24703.796150
25889.784870
28926.032340
30470.016700
Germany
7144.114393
10187.826650
12902.462910
14745.625610
18016.180270
20512.921230
22031.532740
24639.185660
26505.303170
27788.884160
30035.801980
32170.374420
Greece
3530.690067
4916.299889
6017.190733
8513.097016
12724.829570
14195.524280
15268.420890
16120.528390
17541.496340
18747.698140
22514.254800
27538.411880
Hungary
5263.673816
6040.180011
7550.359877
9326.644670
10168.656110
11674.837370
12545.990660
12986.479980
10535.628550
11712.776800
14843.935560
18008.944440
Iceland
7267.688428
9244.001412
10350.159060
13319.895680
15798.063620
19654.962470
23269.607500
26923.206280
25144.392010
28061.099660
31163.201960
36180.789190
Ireland
5210.280328
5599.077872
6631.597314
7655.568963
9530.772896
11150.981130
12618.321410
13872.866520
17558.815550
24521.947130
34077.049390
40675.996350
Italy
4931.404155
6248.656232
8243.582340
10022.401310
12269.273780
14255.984750
16537.483500
19207.234820
22013.644860
24675.024460
27968.098170
28569.719700
Montenegro
2647.585601
3682.259903
4649.593785
5907.850937
7778.414017
9595.929905
11222.587620
11732.510170
7003.339037
6465.613349
6557.194282
9253.896111
Netherlands
8941.571858
11276.193440
12790.849560
15363.251360
18794.745670
21209.059200
21399.460460
23651.323610
26790.949610
30246.130630
33724.757780
36797.933320
Norway
10095.421720
11653.973040
13450.401510
16361.876470
18965.055510
23311.349390
26298.635310
31540.974800
33965.661150
41283.164330
44683.975250
49357.190170
Poland
4029.329699
4734.253019
5338.752143
6557.152776
8006.506993
9508.141454
8451.531004
9082.351172
7738.881247
10159.583680
12002.239080
15389.924680
Portugal
3068.319867
3774.571743
4727.954889
6361.517993
9022.247417
10172.485720
11753.842910
13039.308760
16207.266630
17641.031560
19970.907870
20509.647770
Romania
3144.613186
3943.370225
4734.997586
6470.866545
8011.414402
9356.397240
9605.314053
9696.273295
6598.409903
7346.547557
7885.360081
10808.475610
Serbia
3581.459448
4981.090891
6289.629157
7991.707066
10522.067490
12980.669560
15181.092700
15870.878510
9325.068238
7914.320304
7236.075251
9786.534714
Slovak Republic
5074.659104
6093.262980
7481.107598
8412.902397
9674.167626
10922.664040
11348.545850
12037.267580
9498.467723
12126.230650
13638.778370
18678.314350
Slovenia
4215.041741
5862.276629
7402.303395
9405.489397
12383.486200
15277.030170
17866.721750
18678.534920
14214.716810
17161.107350
20660.019360
25768.257590
Spain
3834.034742
4564.802410
5693.843879
7993.512294
10638.751310
13236.921170
13926.169970
15764.983130
18603.064520
20445.298960
24835.471660
28821.063700
Sweden
8527.844662
9911.878226
12329.441920
15258.296970
17832.024640
18855.725210
20667.381250
23586.929270
23880.016830
25266.594990
29341.630930
33859.748350
Switzerland
14734.232750
17909.489730
20431.092700
22966.144320
27195.113040
26982.290520
28397.715120
30281.704590
31871.530300
32135.323010
34480.957710
37506.419070
Turkey
1969.100980
2218.754257
2322.869908
2826.356387
3450.696380
4269.122326
4241.356344
5089.043686
5678.348271
6601.429915
6508.085718
8458.276384
United Kingdom
9979.508487
11283.177950
12477.177070
14142.850890
15895.116410
17428.748460
18232.424520
21664.787670
22705.092540
26074.531360
29478.999190
33203.261280
In [66]:
'''
For each column in data, idxmin will return the index value corresponding to each column’s minimum
idxmax will do accordingly the same for each column’s maximum value.
You can use these functions whenever you want to get the row index
of the minimum/maximum value and not the actual minimum/maximum value.
'''
print(europe.idxmin())
print(europe.idxmax())
gdpPercap_1952 Bosnia and Herzegovina
gdpPercap_1957 Bosnia and Herzegovina
gdpPercap_1962 Bosnia and Herzegovina
gdpPercap_1967 Bosnia and Herzegovina
gdpPercap_1972 Bosnia and Herzegovina
gdpPercap_1977 Bosnia and Herzegovina
gdpPercap_1982 Albania
gdpPercap_1987 Albania
gdpPercap_1992 Albania
gdpPercap_1997 Albania
gdpPercap_2002 Albania
gdpPercap_2007 Albania
dtype: object
gdpPercap_1952 Switzerland
gdpPercap_1957 Switzerland
gdpPercap_1962 Switzerland
gdpPercap_1967 Switzerland
gdpPercap_1972 Switzerland
gdpPercap_1977 Switzerland
gdpPercap_1982 Switzerland
gdpPercap_1987 Norway
gdpPercap_1992 Norway
gdpPercap_1997 Norway
gdpPercap_2002 Norway
gdpPercap_2007 Norway
dtype: object
In [81]:
help(europe.idxmax)
Help on method idxmax in module pandas.core.frame:
idxmax(axis=0, skipna=True) method of pandas.core.frame.DataFrame instance
Return index of first occurrence of maximum over requested axis.
NA/null values are excluded.
Parameters
----------
axis : {0 or 'index', 1 or 'columns'}, default 0
0 or 'index' for row-wise, 1 or 'columns' for column-wise
skipna : boolean, default True
Exclude NA/null values. If an entire row/column is NA, the result
will be NA.
Raises
------
ValueError
* If the row/column is empty
Returns
-------
idxmax : Series
Notes
-----
This method is the DataFrame version of ``ndarray.argmax``.
See Also
--------
Series.idxmax
In [69]:
'''
Assume Pandas has been imported and the Gapminder GDP data for Europe has been loaded.
Write an expression to select each of the following:
1.- GDP per capita for all countries in 1982.
2.- GDP per capita for Denmark for all years.
3.- GDP per capita for all countries for years after 1985.
4.- GDP per capita for each country in 2007 as a multiple of GDP per capita for that country in 1952.
'''
# 1.- GDP per capita for all countries in 1982.
europe.loc[:, 'gdpPercap_1982']
Out[69]:
country
Albania 3630.880722
Austria 21597.083620
Belgium 20979.845890
Bosnia and Herzegovina 4126.613157
Bulgaria 8224.191647
Croatia 13221.821840
Czech Republic 15377.228550
Denmark 21688.040480
Finland 18533.157610
France 20293.897460
Germany 22031.532740
Greece 15268.420890
Hungary 12545.990660
Iceland 23269.607500
Ireland 12618.321410
Italy 16537.483500
Montenegro 11222.587620
Netherlands 21399.460460
Norway 26298.635310
Poland 8451.531004
Portugal 11753.842910
Romania 9605.314053
Serbia 15181.092700
Slovak Republic 11348.545850
Slovenia 17866.721750
Spain 13926.169970
Sweden 20667.381250
Switzerland 28397.715120
Turkey 4241.356344
United Kingdom 18232.424520
Name: gdpPercap_1982, dtype: float64
In [78]:
# same as result previous but shorter syntax
europe['gdpPercap_1982']
Out[78]:
country
Albania 3630.880722
Austria 21597.083620
Belgium 20979.845890
Bosnia and Herzegovina 4126.613157
Bulgaria 8224.191647
Croatia 13221.821840
Czech Republic 15377.228550
Denmark 21688.040480
Finland 18533.157610
France 20293.897460
Germany 22031.532740
Greece 15268.420890
Hungary 12545.990660
Iceland 23269.607500
Ireland 12618.321410
Italy 16537.483500
Montenegro 11222.587620
Netherlands 21399.460460
Norway 26298.635310
Poland 8451.531004
Portugal 11753.842910
Romania 9605.314053
Serbia 15181.092700
Slovak Republic 11348.545850
Slovenia 17866.721750
Spain 13926.169970
Sweden 20667.381250
Switzerland 28397.715120
Turkey 4241.356344
United Kingdom 18232.424520
Name: gdpPercap_1982, dtype: float64
In [70]:
# 2.- GDP per capita for Denmark for all years.
europe.loc['Denmark']
Out[70]:
gdpPercap_1952 9692.385245
gdpPercap_1957 11099.659350
gdpPercap_1962 13583.313510
gdpPercap_1967 15937.211230
gdpPercap_1972 18866.207210
gdpPercap_1977 20422.901500
gdpPercap_1982 21688.040480
gdpPercap_1987 25116.175810
gdpPercap_1992 26406.739850
gdpPercap_1997 29804.345670
gdpPercap_2002 32166.500060
gdpPercap_2007 35278.418740
Name: Denmark, dtype: float64
In [79]:
# 3.- GDP per capita for all countries for years after 1985.
europe.loc[:, 'gdpPercap_1985':]
Out[79]:
gdpPercap_1987
gdpPercap_1992
gdpPercap_1997
gdpPercap_2002
gdpPercap_2007
country
Albania
3738.932735
2497.437901
3193.054604
4604.211737
5937.029526
Austria
23687.826070
27042.018680
29095.920660
32417.607690
36126.492700
Belgium
22525.563080
25575.570690
27561.196630
30485.883750
33692.605080
Bosnia and Herzegovina
4314.114757
2546.781445
4766.355904
6018.975239
7446.298803
Bulgaria
8239.854824
6302.623438
5970.388760
7696.777725
10680.792820
Croatia
13822.583940
8447.794873
9875.604515
11628.388950
14619.222720
Czech Republic
16310.443400
14297.021220
16048.514240
17596.210220
22833.308510
Denmark
25116.175810
26406.739850
29804.345670
32166.500060
35278.418740
Finland
21141.012230
20647.164990
23723.950200
28204.590570
33207.084400
France
22066.442140
24703.796150
25889.784870
28926.032340
30470.016700
Germany
24639.185660
26505.303170
27788.884160
30035.801980
32170.374420
Greece
16120.528390
17541.496340
18747.698140
22514.254800
27538.411880
Hungary
12986.479980
10535.628550
11712.776800
14843.935560
18008.944440
Iceland
26923.206280
25144.392010
28061.099660
31163.201960
36180.789190
Ireland
13872.866520
17558.815550
24521.947130
34077.049390
40675.996350
Italy
19207.234820
22013.644860
24675.024460
27968.098170
28569.719700
Montenegro
11732.510170
7003.339037
6465.613349
6557.194282
9253.896111
Netherlands
23651.323610
26790.949610
30246.130630
33724.757780
36797.933320
Norway
31540.974800
33965.661150
41283.164330
44683.975250
49357.190170
Poland
9082.351172
7738.881247
10159.583680
12002.239080
15389.924680
Portugal
13039.308760
16207.266630
17641.031560
19970.907870
20509.647770
Romania
9696.273295
6598.409903
7346.547557
7885.360081
10808.475610
Serbia
15870.878510
9325.068238
7914.320304
7236.075251
9786.534714
Slovak Republic
12037.267580
9498.467723
12126.230650
13638.778370
18678.314350
Slovenia
18678.534920
14214.716810
17161.107350
20660.019360
25768.257590
Spain
15764.983130
18603.064520
20445.298960
24835.471660
28821.063700
Sweden
23586.929270
23880.016830
25266.594990
29341.630930
33859.748350
Switzerland
30281.704590
31871.530300
32135.323010
34480.957710
37506.419070
Turkey
5089.043686
5678.348271
6601.429915
6508.085718
8458.276384
United Kingdom
21664.787670
22705.092540
26074.531360
29478.999190
33203.261280
In [80]:
# 4.- GDP per capita for each country in 2007 as a multiple of GDP per capita for that country in 1952.
europe['gdpPercap_2007']/europe['gdpPercap_1952']
Out[80]:
country
Albania 3.708196
Austria 5.886596
Belgium 4.038377
Bosnia and Herzegovina 7.648736
Bulgaria 4.369697
Croatia 4.686795
Czech Republic 3.320658
Denmark 3.639808
Finland 5.168805
France 4.334402
Germany 4.503060
Greece 7.799725
Hungary 3.421364
Iceland 4.978308
Ireland 7.806873
Italy 5.793425
Montenegro 3.495221
Netherlands 4.115376
Norway 4.889067
Poland 3.819475
Portugal 6.684325
Romania 3.437140
Serbia 2.732555
Slovak Republic 3.680703
Slovenia 6.113405
Spain 7.517163
Sweden 3.970493
Switzerland 2.545529
Turkey 4.295502
United Kingdom 3.327144
dtype: float64
In [ ]:
Content source: juancarlosqr/datascience
Similar notebooks: