Python

Python import data, indexing, slicing

––– views

Common Shortcut

Select cell and press: Ctrl-Enter for run selected cells

  • Alt-Enter for run cell and insert below
  • A for insert new cell above selected cell
  • B for insert new cell below selected cell
  • M for make selected cell as markdown

Install Packages

pip install package_name
conda install package_name
use pip in command prompt
conda in Anaconda prompt

Working Directory

%pwd #pwd- Print Working Directory, will give you current working directory
'C:\\Users\\faisal\\Desktop\\Python\\Lesson-1'

Change directory

%cd C:\Users\faisal\Desktop\Python\Lesson-1 #cd- change directory
    [WinError 2] The system cannot find the file specified: 'C:\\Users\\faisa\\Desktop\\Python\\Lesson-1 #cd- change directory'
    C:\Users\faisa\Desktop\Python\Lesson-1

Load Packages

import numpy as np
import pandas as pd
import pyodbc #require for sql server connection

Import csv from Local Machine

df=pd.read_csv('cost_of_living.csv') #press shift Tab to check all available parameter

#df=pd.read_csv('c:\\Users\\faisa\\Desktop\\Python\\Lesson-1\\cost_of_living.csv',header=none)
#df=pd.read_csv(r'c:\Users\faisa\Desktop\Python\Lesson-1\cost_of_living.csv')
df.head()
   Rank                 City  Cost of Living Index  Rent Index  \
0     1    Hamilton, Bermuda                145.43      110.87
1     2  Zurich, Switzerland                141.25       66.14
2     3  Geneva, Switzerland                134.83       71.70
3     4   Basel, Switzerland                130.68       49.68
4     5    Bern, Switzerland                128.03       43.57

   Cost of Living Plus Rent Index  Groceries Index  Restaurant Price Index  \
0                          128.76           143.47                  158.75
1                          105.03           149.86                  135.76
2                          104.38           138.98                  129.74
3                           91.61           127.54                  127.22
4                           87.30           132.70                  119.48

   Local Purchasing Power Index
0                        112.26
1                        142.70
2                        130.96
3                        139.01
4                        112.71

Output CSV

df.to_csv('mydf.csv',index=False) #Don't forget to add '.csv' at the end.
#df.to_csv(r'c:\Users\faisa\Desktop\Python\Lesson-1\my_df.csv',header=True,index=False) #Don't forget to add '.csv' at the end.
#df.to_csv ('C:\\Users\\faisa\\Desktop\\Python\\Lesson-1\\my_df.csv', header=True,index=False) #Don't forget to add '.csv' at the end.

Import xlsx from Local Machine

df_exl=pd.read_excel('cost_of_living_xl.xlsx', sheet_name='sheet1') #specify sheet name from your excel file

Output Excel

df_exl.to_excel('mydf.xlsx',sheet_name='Sheet1')

Import from SQL Server

cnxn = pyodbc.connect("Driver={SQL Server};"
                       "Server=DESKTOP-H3MCNFQ;"
                       "Database=mydb;")
                       # "uid=User;pwd=password")
df_sql = pd.read_sql_query('select * from [cost_of_living_2018]', cnxn)
df_sql.head()
   Rank                 City  Cost_of_Living_Index  Rent_Index  \
0     1    Hamilton, Bermuda                145.43      110.87
1     2  Zurich, Switzerland                141.25       66.14
2     3  Geneva, Switzerland                134.83       71.70
3     4   Basel, Switzerland                130.68       49.68
4     5    Bern, Switzerland                128.03       43.57

   Cost_of_Living_Plus_Rent_Index  Groceries_Index  Restaurant_Price_Index  \
0                          128.76           143.47                  158.75
1                          105.03           149.86                  135.76
2                          104.38           138.98                  129.74
3                           91.61           127.54                  127.22
4                           87.30           132.70                  119.48

   Local_Purchasing_Power_Index
0                        112.26
1                        142.70
2                        130.96
3                        139.01
4                        112.71

Import html Table

may need to install htmllib5,lxml, and BeautifulSoup4 packages:

conda install lxml
conda install html5lib
conda install BeautifulSoup4

df_html=pd.read_html('https://www.contextures.com/xlSampleData01.html',header=0)
df_html[0].head()
   OrderDate   Region      Rep    Item  Units  UnitCost   Total
0   1/6/2018     East    Jones  Pencil     95      1.99  189.05
1  1/23/2018  Central   Kivell  Binder     50     19.99  999.50
2   2/9/2018  Central  Jardine  Pencil     36      4.99  179.64
3  2/26/2018  Central     Gill     Pen     27     19.99  539.73
4  3/15/2018     West  Sorvino  Pencil     56      2.99  167.44

Import Remote Data

df_git = pd.read_csv('https://raw.githubusercontent.com/cs109/2014_data/master/mtcars.csv')
df_git.head()
          Unnamed: 0   mpg  cyl   disp   hp  drat     wt   qsec  vs  am  gear  \
0          Mazda RX4  21.0    6  160.0  110  3.90  2.620  16.46   0   1     4
1      Mazda RX4 Wag  21.0    6  160.0  110  3.90  2.875  17.02   0   1     4
2         Datsun 710  22.8    4  108.0   93  3.85  2.320  18.61   1   1     4
3     Hornet 4 Drive  21.4    6  258.0  110  3.08  3.215  19.44   1   0     3
4  Hornet Sportabout  18.7    8  360.0  175  3.15  3.440  17.02   0   0     3

   carb
0     4
1     4
2     1
3     1
4     2
df_git.to_csv ('C:\\Users\\faisa\\Desktop\\Python\\Lesson-1\\my_df_git.csv', header=True) #Don't forget to add '.csv' at the end.

Basic Information

df.shape
    (538, 8)
df.columns
    Index(['Rank', 'City', 'Cost of Living Index', 'Rent Index',
           'Cost of Living Plus Rent Index', 'Groceries Index',
           'Restaurant Price Index', 'Local Purchasing Power Index'],
          dtype='object')
df.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 538 entries, 0 to 537
    Data columns (total 8 columns):
    Rank                              538 non-null int64
    City                              538 non-null object
    Cost of Living Index              538 non-null float64
    Rent Index                        538 non-null float64
    Cost of Living Plus Rent Index    538 non-null float64
    Groceries Index                   538 non-null float64
    Restaurant Price Index            538 non-null float64
    Local Purchasing Power Index      538 non-null float64
    dtypes: float64(6), int64(1), object(1)
    memory usage: 33.7+ KB
df.count()
    Rank                              538
    City                              538
    Cost of Living Index              538
    Rent Index                        538
    Cost of Living Plus Rent Index    538
    Groceries Index                   538
    Restaurant Price Index            538
    Local Purchasing Power Index      538
    dtype: int64
df.sum()
    Rank                                                                         144991
    City                              Hamilton, BermudaZurich, SwitzerlandGeneva, Sw...
    Cost of Living Index                                                          34220
    Rent Index                                                                  14624.4
    Cost of Living Plus Rent Index                                              24769.5
    Groceries Index                                                             32062.1
    Restaurant Price Index                                                      31733.7
    Local Purchasing Power Index                                                48515.9
    dtype: object
df.min()
    Rank                                            1
    City                              Aachen, Germany
    Cost of Living Index                        20.86
    Rent Index                                   3.82
    Cost of Living Plus Rent Index              13.26
    Groceries Index                             19.26
    Restaurant Price Index                      12.06
    Local Purchasing Power Index                 1.88
    dtype: object
df.max()
    Rank                                              538
    City                              Zurich, Switzerland
    Cost of Living Index                           145.43
    Rent Index                                     115.36
    Cost of Living Plus Rent Index                 128.76
    Groceries Index                                149.86
    Restaurant Price Index                         158.75
    Local Purchasing Power Index                   168.93
    dtype: object
df.describe()
             Rank  Cost of Living Index  Rent Index  \
count  538.000000            538.000000  538.000000
mean   269.500000             63.605874   27.182937
std    155.451493             21.359530   17.207302
min      1.000000             20.860000    3.820000
25%    135.250000             46.060000   13.002500
50%    269.500000             67.805000   25.095000
75%    403.750000             78.430000   35.432500
max    538.000000            145.430000  115.360000

       Cost of Living Plus Rent Index  Groceries Index  \
count                      538.000000       538.000000
mean                        46.039944        59.594926
std                         18.330342        22.168789
min                         13.260000        19.260000
25%                         30.997500        40.477500
50%                         48.030000        61.630000
75%                         58.005000        74.362500
max                        128.760000       149.860000

       Restaurant Price Index  Local Purchasing Power Index
count              538.000000                    538.000000
mean                58.984498                     90.178271
std                 26.243787                     36.637241
min                 12.060000                      1.880000
25%                 34.490000                     58.087500
50%                 64.065000                     95.160000
75%                 77.165000                    120.140000
max                158.750000                    168.930000
df.mean()
    Rank                              269.500000
    Cost of Living Index               63.605874
    Rent Index                         27.182937
    Cost of Living Plus Rent Index     46.039944
    Groceries Index                    59.594926
    Restaurant Price Index             58.984498
    Local Purchasing Power Index       90.178271
    dtype: float64
df.median()
    Rank                              269.500
    Cost of Living Index               67.805
    Rent Index                         25.095
    Cost of Living Plus Rent Index     48.030
    Groceries Index                    61.630
    Restaurant Price Index             64.065
    Local Purchasing Power Index       95.160
    dtype: float64
#df.isna() #will return True or False for each value, if null then True, if not null then False
df.isna().sum() #will return total number of null for each column
    Rank                              0
    City                              0
    Cost of Living Index              0
    Rent Index                        0
    Cost of Living Plus Rent Index    0
    Groceries Index                   0
    Restaurant Price Index            0
    Local Purchasing Power Index      0
    dtype: int64

Basic Indexing and Selecting and Slicing

df['City']
    0                          Hamilton, Bermuda
    1                        Zurich, Switzerland
    2                        Geneva, Switzerland
    3                         Basel, Switzerland
    4                          Bern, Switzerland
    5                      Lausanne, Switzerland
    6                         Reykjavik, Iceland
    7                          Stavanger, Norway
    8                        Lugano, Switzerland
    9                               Oslo, Norway
    10                         Trondheim, Norway
    11                            Bergen, Norway
    12                              Kyoto, Japan
    13               New York, NY, United States
    14                           Nassau, Bahamas
    15          San Francisco, CA, United States
    16                       Copenhagen, Denmark
    17                    Luxembourg, Luxembourg
    18              Anchorage, AK, United States
    19               Honolulu, HI, United States
    20                              Tokyo, Japan
    21               Brooklyn, NY, United States
    22                             Paris, France
    23                         Limerick, Ireland
    24              Rockville, MD, United States
    25            Bloomington, IN, United States
    26             Washington, DC, United States
    27                            Arhus, Denmark
    28                      Singapore, Singapore
    29                          Aalborg, Denmark
                           ...
    508                         Lahore, Pakistan
    509    Pristina, Kosovo (Disputed Territory)
    510                        Chandigarh, India
    511                         Ahmedabad, India
    512                             Surat, India
    513                           Chennai, India
    514                               Goa, India
    515                            Indore, India
    516                           Kolkata, India
    517                 Lucknow (Lakhnau), India
    518                            Kiev, Ukraine
    519                            Jaipur, India
    520                        Karachi, Pakistan
    521                         Hyderabad, India
    522                             Cairo, Egypt
    523                          Dnipro, Ukraine
    524                            Nagpur, India
    525                            Bhopal, India
    526                          Vadodara, India
    527                         Mangalore, India
    528                            Lviv, Ukraine
    529                            Mysore, India
    530                       Bhubaneswar, India
    531                         Kharkiv, Ukraine
    532                     Visakhapatnam, India
    533                             Kochi, India
    534                        Coimbatore, India
    535                        Alexandria, Egypt
    536                       Navi Mumbai, India
    537                Thiruvananthapuram, India
    Name: City, Length: 538, dtype: object
df[['City','Restaurant Price Index']]
                                      City  Restaurant Price Index
0                        Hamilton, Bermuda                  158.75
1                      Zurich, Switzerland                  135.76
2                      Geneva, Switzerland                  129.74
3                       Basel, Switzerland                  127.22
4                        Bern, Switzerland                  119.48
5                    Lausanne, Switzerland                  132.12
6                       Reykjavik, Iceland                  133.19
7                        Stavanger, Norway                  143.54
8                      Lugano, Switzerland                  122.30
9                             Oslo, Norway                  124.09
10                       Trondheim, Norway                  134.76
11                          Bergen, Norway                  119.61
12                            Kyoto, Japan                   54.59
13             New York, NY, United States                  100.00
14                         Nassau, Bahamas                  104.17
15        San Francisco, CA, United States                   91.06
16                     Copenhagen, Denmark                  121.23
17                  Luxembourg, Luxembourg                  109.61
18            Anchorage, AK, United States                   84.55
19             Honolulu, HI, United States                   82.86
20                            Tokyo, Japan                   58.93
21             Brooklyn, NY, United States                  100.58
22                           Paris, France                   91.77
23                       Limerick, Ireland                   82.93
24            Rockville, MD, United States                   74.74
25          Bloomington, IN, United States                   75.43
26           Washington, DC, United States                   85.00
27                          Arhus, Denmark                  102.82
28                    Singapore, Singapore                   64.40
29                        Aalborg, Denmark                  101.14
..                                     ...                     ...
508                       Lahore, Pakistan                   26.39
509  Pristina, Kosovo (Disputed Territory)                   22.78
510                      Chandigarh, India                   20.18
511                       Ahmedabad, India                   20.13
512                           Surat, India                   19.84
513                         Chennai, India                   18.26
514                             Goa, India                   22.96
515                          Indore, India                   17.77
516                         Kolkata, India                   23.18
517               Lucknow (Lakhnau), India                   18.76
518                          Kiev, Ukraine                   22.01
519                          Jaipur, India                   18.48
520                      Karachi, Pakistan                   21.62
521                       Hyderabad, India                   18.93
522                           Cairo, Egypt                   22.55
523                        Dnipro, Ukraine                   22.74
524                          Nagpur, India                   18.73
525                          Bhopal, India                   16.21
526                        Vadodara, India                   16.02
527                       Mangalore, India                   16.04
528                          Lviv, Ukraine                   17.88
529                          Mysore, India                   13.31
530                     Bhubaneswar, India                   14.91
531                       Kharkiv, Ukraine                   18.44
532                   Visakhapatnam, India                   18.07
533                           Kochi, India                   13.94
534                      Coimbatore, India                   15.21
535                      Alexandria, Egypt                   17.66
536                     Navi Mumbai, India                   14.14
537              Thiruvananthapuram, India                   12.06

[538 rows x 2 columns]
df[2:10] #specific rows, all columns
   Rank                   City  Cost of Living Index  Rent Index  \
2     3    Geneva, Switzerland                134.83       71.70
3     4     Basel, Switzerland                130.68       49.68
4     5      Bern, Switzerland                128.03       43.57
5     6  Lausanne, Switzerland                127.50       52.32
6     7     Reykjavik, Iceland                123.78       57.25
7     8      Stavanger, Norway                118.61       39.83
8     9    Lugano, Switzerland                118.24       52.91
9    10           Oslo, Norway                117.23       49.28

   Cost of Living Plus Rent Index  Groceries Index  Restaurant Price Index  \
2                          104.38           138.98                  129.74
3                           91.61           127.54                  127.22
4                           87.30           132.70                  119.48
5                           91.24           126.59                  132.12
6                           91.70           118.15                  133.19
7                           80.61           106.09                  143.54
8                           86.73           117.74                  122.30
9                           84.46           112.42                  124.09

   Local Purchasing Power Index
2                        130.96
3                        139.01
4                        112.71
5                        127.95
6                         88.95
7                        118.14
8                        119.86
9                        102.94
#.at labels based
df.at[3,'Rent Index']
    49.68
#.iat integer based
df.iat[3,3]
    49.68
df.head(10)
   Rank                   City  Cost of Living Index  Rent Index  \
0     1      Hamilton, Bermuda                145.43      110.87
1     2    Zurich, Switzerland                141.25       66.14
2     3    Geneva, Switzerland                134.83       71.70
3     4     Basel, Switzerland                130.68       49.68
4     5      Bern, Switzerland                128.03       43.57
5     6  Lausanne, Switzerland                127.50       52.32
6     7     Reykjavik, Iceland                123.78       57.25
7     8      Stavanger, Norway                118.61       39.83
8     9    Lugano, Switzerland                118.24       52.91
9    10           Oslo, Norway                117.23       49.28

   Cost of Living Plus Rent Index  Groceries Index  Restaurant Price Index  \
0                          128.76           143.47                  158.75
1                          105.03           149.86                  135.76
2                          104.38           138.98                  129.74
3                           91.61           127.54                  127.22
4                           87.30           132.70                  119.48
5                           91.24           126.59                  132.12
6                           91.70           118.15                  133.19
7                           80.61           106.09                  143.54
8                           86.73           117.74                  122.30
9                           84.46           112.42                  124.09

   Local Purchasing Power Index
0                        112.26
1                        142.70
2                        130.96
3                        139.01
4                        112.71
5                        127.95
6                         88.95
7                        118.14
8                        119.86
9                        102.94
#loc is label based
#select specific rows and column
df.loc[:,['City', 'Cost of Living Index', 'Rent Index',
       'Cost of Living Plus Rent Index', 'Groceries Index',
       'Restaurant Price Index', 'Local Purchasing Power Index']]
                                      City  Cost of Living Index  Rent Index  \
0                        Hamilton, Bermuda                145.43      110.87
1                      Zurich, Switzerland                141.25       66.14
2                      Geneva, Switzerland                134.83       71.70
3                       Basel, Switzerland                130.68       49.68
4                        Bern, Switzerland                128.03       43.57
5                    Lausanne, Switzerland                127.50       52.32
6                       Reykjavik, Iceland                123.78       57.25
7                        Stavanger, Norway                118.61       39.83
8                      Lugano, Switzerland                118.24       52.91
9                             Oslo, Norway                117.23       49.28
10                       Trondheim, Norway                114.22       42.39
11                          Bergen, Norway                112.31       40.30
12                            Kyoto, Japan                100.33       24.58
13             New York, NY, United States                100.00      100.00
14                         Nassau, Bahamas                 99.73       40.45
15        San Francisco, CA, United States                 97.84      115.36
16                     Copenhagen, Denmark                 97.62       50.66
17                  Luxembourg, Luxembourg                 95.37       61.59
18            Anchorage, AK, United States                 94.99       40.12
19             Honolulu, HI, United States                 94.15       62.82
20                            Tokyo, Japan                 93.81       37.07
21             Brooklyn, NY, United States                 93.79       76.24
22                           Paris, France                 92.87       50.30
23                       Limerick, Ireland                 92.73       27.71
24            Rockville, MD, United States                 92.66       64.00
25          Bloomington, IN, United States                 92.14       33.64
26           Washington, DC, United States                 91.94       73.30
27                          Arhus, Denmark                 91.90       34.82
28                    Singapore, Singapore                 91.40       71.89
29                        Aalborg, Denmark                 91.17       26.81
..                                     ...                   ...         ...
508                       Lahore, Pakistan                 29.53        6.67
509  Pristina, Kosovo (Disputed Territory)                 29.25        9.38
510                      Chandigarh, India                 29.04        6.47
511                       Ahmedabad, India                 28.67        6.24
512                           Surat, India                 28.66        4.69
513                         Chennai, India                 28.42        7.12
514                             Goa, India                 28.30        8.27
515                          Indore, India                 28.06        4.66
516                         Kolkata, India                 27.99        7.77
517               Lucknow (Lakhnau), India                 27.55        4.90
518                          Kiev, Ukraine                 27.52       12.43
519                          Jaipur, India                 27.11        5.19
520                      Karachi, Pakistan                 27.10        7.46
521                       Hyderabad, India                 26.92        6.89
522                           Cairo, Egypt                 26.49        5.43
523                        Dnipro, Ukraine                 26.39        6.63
524                          Nagpur, India                 26.23        4.96
525                          Bhopal, India                 26.07        4.13
526                        Vadodara, India                 25.59        4.01
527                       Mangalore, India                 25.46        5.70
528                          Lviv, Ukraine                 25.31        8.10
529                          Mysore, India                 25.20        4.01
530                     Bhubaneswar, India                 24.89        4.68
531                       Kharkiv, Ukraine                 24.85        8.29
532                   Visakhapatnam, India                 24.66        4.85
533                           Kochi, India                 24.65        6.31
534                      Coimbatore, India                 24.61        5.35
535                      Alexandria, Egypt                 23.78        4.34
536                     Navi Mumbai, India                 23.44        6.25
537              Thiruvananthapuram, India                 20.86        5.10

     Cost of Living Plus Rent Index  Groceries Index  Restaurant Price Index  \
0                            128.76           143.47                  158.75
1                            105.03           149.86                  135.76
2                            104.38           138.98                  129.74
3                             91.61           127.54                  127.22
4                             87.30           132.70                  119.48
5                             91.24           126.59                  132.12
6                             91.70           118.15                  133.19
7                             80.61           106.09                  143.54
8                             86.73           117.74                  122.30
9                             84.46           112.42                  124.09
10                            79.58           103.50                  134.76
11                            77.58           101.79                  119.61
12                            63.80           118.44                   54.59
13                           100.00           100.00                  100.00
14                            71.14            85.34                  104.17
15                           106.29           107.52                   91.06
16                            74.97            77.53                  121.23
17                            79.08            82.71                  109.61
18                            68.53           101.18                   84.55
19                            79.04           104.69                   82.86
20                            66.45            99.67                   58.93
21                            85.33            92.73                  100.58
22                            72.34            87.29                   91.77
23                            61.37            87.15                   82.93
24                            78.84            87.76                   74.74
25                            63.93           112.83                   75.43
26                            82.95            92.74                   85.00
27                            64.37            71.50                  102.82
28                            81.99            83.64                   64.40
29                            60.13            73.79                  101.14
..                              ...              ...                     ...
508                           18.50            26.83                   26.39
509                           19.67            25.97                   22.78
510                           18.15            29.40                   20.18
511                           17.85            31.42                   20.13
512                           17.10            31.97                   19.84
513                           18.14            31.17                   18.26
514                           18.64            29.80                   22.96
515                           16.78            27.74                   17.77
516                           18.24            28.53                   23.18
517                           16.62            27.25                   18.76
518                           20.24            21.96                   22.01
519                           16.54            27.65                   18.48
520                           17.63            25.60                   21.62
521                           17.26            27.60                   18.93
522                           16.33            23.23                   22.55
523                           16.86            20.46                   22.74
524                           15.97            26.55                   18.73
525                           15.49            22.49                   16.21
526                           15.18            27.85                   16.02
527                           15.93            26.85                   16.04
528                           17.01            20.50                   17.88
529                           14.98            29.39                   13.31
530                           15.14            28.22                   14.91
531                           16.87            19.26                   18.44
532                           15.11            25.83                   18.07
533                           15.80            26.93                   13.94
534                           15.32            25.23                   15.21
535                           14.40            23.19                   17.66
536                           15.15            24.02                   14.14
537                           13.26            21.98                   12.06

     Local Purchasing Power Index
0                          112.26
1                          142.70
2                          130.96
3                          139.01
4                          112.71
5                          127.95
6                           88.95
7                          118.14
8                          119.86
9                          102.94
10                         108.29
11                          99.29
12                          77.92
13                         100.00
14                          58.69
15                          92.96
16                         113.31
17                         127.42
18                         124.92
19                         103.08
20                         106.42
21                          87.04
22                          97.62
23                          93.93
24                         130.79
25                          96.92
26                         120.62
27                         109.47
28                          95.89
29                         106.35
..                            ...
508                         51.44
509                         64.57
510                         68.83
511                         73.59
512                         57.84
513                         72.34
514                         54.55
515                         50.42
516                         56.30
517                         76.10
518                         37.48
519                         76.50
520                         39.06
521                         80.90
522                         25.27
523                         31.06
524                         95.19
525                         66.21
526                         80.63
527                         94.53
528                         26.88
529                         42.49
530                         57.56
531                         27.19
532                         63.97
533                         77.70
534                         53.23
535                         23.75
536                        111.99
537                         66.25

[538 rows x 7 columns]
#select all rows but specific column
df.loc[[0,3],['City', 'Cost of Living Index', 'Rent Index',
       'Cost of Living Plus Rent Index', 'Groceries Index',
       'Restaurant Price Index', 'Local Purchasing Power Index']]
                 City  Cost of Living Index  Rent Index  \
0   Hamilton, Bermuda                145.43      110.87
3  Basel, Switzerland                130.68       49.68

   Cost of Living Plus Rent Index  Groceries Index  Restaurant Price Index  \
0                          128.76           143.47                  158.75
3                           91.61           127.54                  127.22

   Local Purchasing Power Index
0                        112.26
3                        139.01
#iloc is integer based
#Specific rows but all columns, remember here last index number is excluding
df.iloc[:5]
   Rank                 City  Cost of Living Index  Rent Index  \
0     1    Hamilton, Bermuda                145.43      110.87
1     2  Zurich, Switzerland                141.25       66.14
2     3  Geneva, Switzerland                134.83       71.70
3     4   Basel, Switzerland                130.68       49.68
4     5    Bern, Switzerland                128.03       43.57

   Cost of Living Plus Rent Index  Groceries Index  Restaurant Price Index  \
0                          128.76           143.47                  158.75
1                          105.03           149.86                  135.76
2                          104.38           138.98                  129.74
3                           91.61           127.54                  127.22
4                           87.30           132.70                  119.48

   Local Purchasing Power Index
0                        112.26
1                        142.70
2                        130.96
3                        139.01
4                        112.71
#Select all rows and specific column, remember here last index number is excluding
df.iloc[:,2:5]
     Cost of Living Index  Rent Index  Cost of Living Plus Rent Index
0                  145.43      110.87                          128.76
1                  141.25       66.14                          105.03
2                  134.83       71.70                          104.38
3                  130.68       49.68                           91.61
4                  128.03       43.57                           87.30
5                  127.50       52.32                           91.24
6                  123.78       57.25                           91.70
7                  118.61       39.83                           80.61
8                  118.24       52.91                           86.73
9                  117.23       49.28                           84.46
10                 114.22       42.39                           79.58
11                 112.31       40.30                           77.58
12                 100.33       24.58                           63.80
13                 100.00      100.00                          100.00
14                  99.73       40.45                           71.14
15                  97.84      115.36                          106.29
16                  97.62       50.66                           74.97
17                  95.37       61.59                           79.08
18                  94.99       40.12                           68.53
19                  94.15       62.82                           79.04
20                  93.81       37.07                           66.45
21                  93.79       76.24                           85.33
22                  92.87       50.30                           72.34
23                  92.73       27.71                           61.37
24                  92.66       64.00                           78.84
25                  92.14       33.64                           63.93
26                  91.94       73.30                           82.95
27                  91.90       34.82                           64.37
28                  91.40       71.89                           81.99
29                  91.17       26.81                           60.13
..                    ...         ...                             ...
508                 29.53        6.67                           18.50
509                 29.25        9.38                           19.67
510                 29.04        6.47                           18.15
511                 28.67        6.24                           17.85
512                 28.66        4.69                           17.10
513                 28.42        7.12                           18.14
514                 28.30        8.27                           18.64
515                 28.06        4.66                           16.78
516                 27.99        7.77                           18.24
517                 27.55        4.90                           16.62
518                 27.52       12.43                           20.24
519                 27.11        5.19                           16.54
520                 27.10        7.46                           17.63
521                 26.92        6.89                           17.26
522                 26.49        5.43                           16.33
523                 26.39        6.63                           16.86
524                 26.23        4.96                           15.97
525                 26.07        4.13                           15.49
526                 25.59        4.01                           15.18
527                 25.46        5.70                           15.93
528                 25.31        8.10                           17.01
529                 25.20        4.01                           14.98
530                 24.89        4.68                           15.14
531                 24.85        8.29                           16.87
532                 24.66        4.85                           15.11
533                 24.65        6.31                           15.80
534                 24.61        5.35                           15.32
535                 23.78        4.34                           14.40
536                 23.44        6.25                           15.15
537                 20.86        5.10                           13.26

[538 rows x 3 columns]

Last updated on