{"id":5198,"date":"2021-05-10T09:32:34","date_gmt":"2021-05-10T04:02:34","guid":{"rendered":"https:\/\/python-programs.com\/?p=5198"},"modified":"2021-11-22T18:42:48","modified_gmt":"2021-11-22T13:12:48","slug":"select-rows-columns-by-name-or-index-in-dataframe-using-loc-iloc-python-pandas","status":"publish","type":"post","link":"https:\/\/python-programs.com\/select-rows-columns-by-name-or-index-in-dataframe-using-loc-iloc-python-pandas\/","title":{"rendered":"Select Rows & Columns by Name or Index in DataFrame using loc & iloc | Python Pandas"},"content":{"rendered":"
We will discuss several methods to select rows and columns in a dataframe. To select rows or columns we can use To demonstrate the various methods we will be using the following dataset :<\/p>\n We can use the dataFrame.loc[<ROWS RANGE> , <COLUMNS RANGE>]<\/em><\/p>\n We have to enter the range of rows or columns, and it will select the specified range.<\/p>\n If we don\u2019t give a value and pass \u2018:\u2019 instead, it will select all the rows or columns.<\/p>\n As we need to select a single column only, we have to pass So, let’s see the implementation of it.<\/p>\n To select multiple columns, we have to pass the column names as a list into the function.<\/p>\n So, let’s see the implementation of it.<\/p>\n Just like the column, we can also select a single row by passing its name and in place of column range passing So, let’s see the implementation of it.<\/p>\n To select multiple rows we have to pass the names as a list into the function.<\/p>\n So, let’s see the implementation of it.<\/p>\n To select multiple rows and columns we have to pass the list of rows and columns we want to select into the function.<\/p>\n So, let’s see the implementation of it.<\/p>\n We can use the dataFrame.iloc<\/em><\/p>\n The function selects rows and columns in the dataframe by the index position we pass into the program. And just as like loc( ) if \u2018 We have to pass the index of the column with So, let’s see the implementation of it.<\/p>\n To select multiple columns by indices we just pass the indices as series into the column value.<\/p>\n So, let’s see the implementation of it.<\/p>\n To select multiple columns by index range we just pass the indices as series into the column value.<\/p>\n So, let’s see the implementation of it.<\/p>\n Just like columns we can pass the index and select the row.<\/p>\n So, let’s see the implementation of it.<\/p>\n To do this we can pass the indices of positions to select into the function.<\/p>\n So, let’s see the implementation of it.<\/p>\n To select a range of rows we pass the range separated by a \u2018:\u2019 into the function.<\/p>\n So, let’s see the implementation of it.<\/p>\n To select multiple rows and columns at once, we pass the indices directly into function.<\/p>\n So, let’s see the implementation of it.<\/p>\n The So, let’s see the implementation of it.<\/p>\n To select multiple columns we just pass a list of their names into So, let’s see the implementation of it.<\/p>\n Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n Read more Articles on Python Data Analysis Using Padas \u2013 Select items from a Dataframe<\/strong><\/p>\n How to select rows and columns by Name or Index in DataFrame using loc and iloc in Python ? We will discuss several methods to select rows and columns in a dataframe. To select rows or columns we can use loc( ), iloc( ) or using the [ ] operator. To demonstrate the various methods …<\/p>\nloc( )<\/code>,
iloc( )<\/code> or using the
[ ] operator<\/code>.<\/p>\n
\u00a0 \u00a0 Name\u00a0 \u00a0 \u00a0 Score\u00a0 \u00a0 \u00a0 \u00a0 City\r\n0\u00a0\u00a0\u00a0\u00a0 Jill\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a016.0\u00a0 \u00a0 \u00a0 \u00a0Tokyo\r\n1\u00a0 \u00a0 \u00a0Rachel\u00a0 \u00a0 \u00a038.0\u00a0 \u00a0 \u00a0 Texas\r\n2\u00a0 \u00a0 \u00a0Kirti\u00a0 \u00a0 \u00a0 \u00a0 \u00a039.0\u00a0 \u00a0 \u00a0 \u00a0New York\r\n3\u00a0 \u00a0 \u00a0Veena\u00a0 \u00a0 \u00a0 40.0\u00a0 \u00a0 \u00a0 Texas\r\n4\u00a0 \u00a0 \u00a0Lucifer\u00a0 \u00a0 \u00a0NaN\u00a0 \u00a0 \u00a0 Texas\r\n5\u00a0 \u00a0 \u00a0Pablo\u00a0 \u00a0 \u00a0 \u00a030.0\u00a0 \u00a0 \u00a0 \u00a0New York\r\n6\u00a0 \u00a0 \u00a0Lionel\u00a0 \u00a0 \u00a0 45.0\u00a0 \u00a0 \u00a0 \u00a0Colombia<\/pre>\n
Method-1 : DataFrame.loc | Select Column & Rows by Name<\/h3>\n
loc( )<\/code> function to select rows and columns.<\/p>\n
Syntax :<\/pre>\n
Select a Column by Name in DataFrame using loc[ ] :<\/h4>\n
\u2018:\u2019<\/code> in row range place.<\/p>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'])\r\n#Selecting the 'Score solumn'\r\ncolumnD = dfObj.loc[:,'Score']\r\nprint(columnD)<\/pre>\n
Output :\r\n0\u00a0\u00a0\u00a0 16.0\r\n1\u00a0\u00a0\u00a0 38.0\r\n2\u00a0\u00a0\u00a0 39.0\r\n3\u00a0\u00a0\u00a0 40.0\r\n4\u00a0\u00a0\u00a0\u00a0 NaN\r\n5\u00a0\u00a0\u00a0 30.0\r\n6\u00a0\u00a0\u00a0 45.0\r\nName: Score, dtype: float64<\/pre>\n
Select multiple Columns by Name in DataFrame using loc[ ] :<\/h4>\n
#Program\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple columns i.e 'Name' and 'Score' column\r\ncolumnD = dfObj.loc[:,['Name','Score']]\r\nprint(columnD)\r\n<\/pre>\n
Output :\r\n \u00a0 \u00a0Name\u00a0 \u00a0 \u00a0 Score\r\na\u00a0\u00a0\u00a0\u00a0 Jill\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 16.0\r\nb\u00a0\u00a0 Rachel\u00a0 \u00a0 \u00a038.0\r\nc\u00a0\u00a0\u00a0 Kirti\u00a0 \u00a0 \u00a0 \u00a0 \u00a039.0\r\nd\u00a0\u00a0\u00a0 Veena\u00a0 \u00a0 \u00a040.0\r\ne\u00a0 Lucifer\u00a0 \u00a0 \u00a0 NaN\r\nf\u00a0\u00a0\u00a0 Pablo\u00a0 \u00a0 \u00a0 30.0\r\ng\u00a0\u00a0 Lionel\u00a0 \u00a0 \u00a0 45.0<\/pre>\n
Select a single row by Index Label in DataFrame using loc[ ] :<\/h4>\n
\u2018:\u2019<\/code>.<\/p>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting a single row i.e 'b' row\r\nselectData = dfObj.loc['b',:]\r\nprint(selectData)\r\n\r\n<\/pre>\n
Output :\r\nName\u00a0\u00a0\u00a0\u00a0 Rachel\r\nScore\u00a0\u00a0\u00a0\u00a0\u00a0 38.0\r\nCity\u00a0\u00a0\u00a0\u00a0\u00a0 Texas\r\nName: b, dtype: object<\/pre>\n
Select multiple rows by Index labels in DataFrame using loc[ ] :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple rows i.e 'd' and 'g'\r\nselectData = dfObj.loc[['d','g'],:]\r\nprint(selectData)\r\n\r\n<\/pre>\n
Output :\r\n Name\u00a0 Score\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\nd\u00a0\u00a0 Veena\u00a0\u00a0 40.0\u00a0\u00a0\u00a0\u00a0 Texas\r\ng\u00a0 Lionel\u00a0\u00a0 45.0\u00a0 Colombia<\/pre>\n
Select multiple row & columns by Labels in DataFrame using loc[ ] :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple rows and columns i.e 'd' and 'g' rows and 'Name' , 'City' column\r\nselectData = dfObj.loc[['d','g'],['Name','City']]\r\nprint(selectData)\r\n\r\n<\/pre>\n
Output :\r\n Name\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\nd\u00a0\u00a0 Veena\u00a0\u00a0\u00a0\u00a0 Texas\r\ng\u00a0 Lionel\u00a0 Colombia<\/pre>\n
Method-2 : DataFrame.iloc | Select Column Indexes & Rows Index Positions<\/h3>\n
iloc( )<\/code> function to select rows and columns. It is quite similar to
loc( )<\/code> function .<\/p>\n
Syntax-<\/pre>\n
[<ROWS INDEX RANGE> , <COLUMNS INDEX RANGE>]<\/pre>\n
:<\/code>\u2019 is passed into the function, all the rows\/columns are selected.<\/p>\n
Select a single column by Index position :<\/h4>\n
\u2018:\u2019<\/code> in place of the row index.<\/p>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting a single column at the index 2\r\nselectData = dfObj.iloc[:,2]\r\nprint(selectData)\r\n<\/pre>\n
Output :\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Tokyo\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Texas\r\nc\u00a0 \u00a0 \u00a0 \u00a0 New York\r\nd\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Texas\r\ne\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Texas\r\nf\u00a0 \u00a0 \u00a0 \u00a0 New York\r\ng\u00a0 \u00a0 \u00a0 \u00a0Colombia\r\nName: City, dtype: object<\/pre>\n
Select multiple columns by Indices in a list :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple columns at the index 0 & 2\r\nselectData = dfObj.iloc[:,[0,2]]\r\nprint(selectData)<\/pre>\n
Output :\r\n \u00a0 \u00a0 \u00a0 Name\u00a0 \u00a0 \u00a0 \u00a0City\r\na\u00a0 \u00a0 \u00a0 Jill\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Tokyo\r\nb\u00a0 \u00a0 \u00a0 Rachel\u00a0\u00a0\u00a0\u00a0 Texas\r\nc\u00a0 \u00a0 \u00a0 Kirti\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 New York\r\nd\u00a0 \u00a0 \u00a0 Veena\u00a0 \u00a0 \u00a0 Texas\r\ne\u00a0 \u00a0 \u00a0 Lucifer\u00a0\u00a0\u00a0\u00a0 Texas\r\nf\u00a0 \u00a0 \u00a0 Pablo\u00a0 \u00a0 \u00a0 \u00a0 New York\r\ng\u00a0 \u00a0 \u00a0Lionel\u00a0 \u00a0 \u00a0 \u00a0Colombia<\/pre>\n
Select multiple columns by Index range :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple columns from the index 1 to 3\r\nselectData = dfObj.iloc[:,1:3]\r\nprint(selectData)\r\n<\/pre>\n
Output :\r\n \u00a0 \u00a0 Score\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\na\u00a0 \u00a0 16.0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Tokyo\r\nb\u00a0 \u00a0 38.0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Texas\r\nc\u00a0 \u00a0 39.0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0New York\r\nd\u00a0 \u00a0 40.0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Texas\r\ne\u00a0\u00a0\u00a0 NaN\u00a0 \u00a0 \u00a0 \u00a0 Texas\r\nf\u00a0 \u00a0 \u00a030.0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0New York\r\ng\u00a0 \u00a0 45.0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Colombia<\/pre>\n
Select single row by Index Position :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting a single row with index 2\r\nselectData = dfObj.iloc[2,:]\r\nprint(selectData)<\/pre>\n
Output :\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Kirti\r\nScore\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 39.0\r\nCity\u00a0\u00a0\u00a0\u00a0 New York\r\nName: c, dtype: object<\/pre>\n
Select multiple rows by Index positions in a list :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple rows by passing alist i.e. 2 & 5\r\nselectData = dfObj.iloc[[2,5],:]\r\nprint(selectData)\r\n<\/pre>\n
Output :\r\n \u00a0 \u00a0Name\u00a0 \u00a0 Score\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\nc\u00a0 \u00a0Kirti\u00a0 \u00a0 \u00a0 39.0\u00a0 \u00a0 \u00a0 \u00a0New York\r\nf\u00a0 \u00a0 Pablo\u00a0\u00a0 30.0\u00a0 \u00a0 \u00a0 \u00a0New York<\/pre>\n
Select multiple rows by Index range :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple rows by range i.e. 2 to 5\r\nselectData = dfObj.iloc[2:5,:]\r\nprint(selectData)\r\n<\/pre>\n
Output :\r\n Name\u00a0 Score\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\nc\u00a0\u00a0\u00a0 Kirti\u00a0\u00a0 39.0\u00a0 New York\r\nd\u00a0\u00a0\u00a0 Veena\u00a0\u00a0 40.0\u00a0\u00a0\u00a0\u00a0 Texas\r\ne\u00a0 Lucifer\u00a0\u00a0\u00a0 NaN\u00a0\u00a0\u00a0\u00a0 Texas<\/pre>\n
Select multiple rows & columns by Index positions :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Selecting multiple rows and columns\r\nselectData = dfObj.iloc[[1,2],[1,2]]\r\nprint(selectData)\r\n<\/pre>\n
Output :\r\n Score\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\nb\u00a0\u00a0 38.0\u00a0\u00a0\u00a0\u00a0 Texas\r\nc\u00a0\u00a0 39.0\u00a0 New York<\/pre>\n
Method-3 : Selecting Columns in DataFrame using [ ] operator<\/h3>\n
[ ]<\/code> operator selects the data according to the name provided to it. However, when a non-existent label is passed into it, it sends a
KeyError<\/code>.<\/p>\n
Select a Column by Name :<\/h4>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Select a single column name using [ ]\r\nselectData = dfObj['Name']\r\nprint(selectData)\r\n<\/pre>\n
Output :\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Jill\r\nb\u00a0\u00a0\u00a0\u00a0 Rachel\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0 Kirti\r\nd\u00a0\u00a0\u00a0\u00a0\u00a0 Veena\r\ne\u00a0\u00a0\u00a0 Lucifer\r\nf\u00a0\u00a0\u00a0\u00a0\u00a0 Pablo\r\ng\u00a0\u00a0\u00a0\u00a0 Lionel\r\nName: Name, dtype: object<\/pre>\n
Select multiple columns by Name :<\/h4>\n
[ ]<\/code>.<\/p>\n
#Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#data\r\nstudents = [\r\n('Jill', 16, 'Tokyo',),\r\n('Rachel', 38, 'Texas',),\r\n('Kirti', 39, 'New York'),\r\n('Veena', 40, 'Texas',),\r\n('Lucifer', np.NaN, 'Texas'),\r\n('Pablo', 30, 'New York'),\r\n('Lionel', 45, 'Colombia',)]\r\n#Creating the dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Name','Score','City'], index=['a','b','c','d','e','f','g'])\r\n#Select multiple columns using [ ]\r\nselectData = dfObj[['Name','City']]\r\nprint(selectData)\r\n<\/pre>\n
Output :\r\n \u00a0 \u00a0 \u00a0Name\u00a0 \u00a0 \u00a0 \u00a0 City\r\na\u00a0 \u00a0 \u00a0 Jill\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Tokyo\r\nb\u00a0 \u00a0 \u00a0 Rachel\u00a0\u00a0\u00a0\u00a0 Texas\r\nc\u00a0 \u00a0 \u00a0 Kirti\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 New York\r\nd\u00a0 \u00a0 \u00a0 Veena\u00a0 \u00a0 \u00a0 \u00a0Texas\r\ne\u00a0 \u00a0 \u00a0 Lucifer\u00a0 \u00a0 \u00a0 Texas\r\nf\u00a0 \u00a0 \u00a0 \u00a0Pablo\u00a0 \u00a0 \u00a0 \u00a0 New York\r\ng\u00a0 \u00a0 \u00a0 Lionel\u00a0 \u00a0 \u00a0 \u00a0Colombia<\/pre>\n
\n