Выберите строки и столбцы по имени или индексу в Pandas DataFrame, используя [], loc & iloc
Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular number of columns, a particular number of rows, and all the columns or a particular number of rows and columns each. Indexing is also known as Subset selection.
Let’s create a simple dataframe with a list of tuples, say column names are: ‘Name’, ‘Age’, ‘City’ and ‘Salary’.
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"])# Show the dataframedf |
Выход: 
Метод 1: использование Dataframe. [].
[] используется для выбора столбца путем упоминания имени соответствующего столбца.
Example 1 : to select single column.
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Using the operator [] # to select a columnresult = df["City"] # Show the dataframeresult |
Выход: 
Example 2: to select multiple columns.
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Using the operator [] to # select multiple columnsresult = df[["Name", "Age", "Salary"]] # Show the dataframeresult |
Выход:
Method 2: Using Dataframe.loc[ ].
.loc[] the function selects the data by labels of rows or columns. It can select a subset of rows and columns. There are many ways to use this function.
Example 1: To select single row.
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Set "Name" column as index # on a Dataframedf.set_index("Name", inplace = True) # Using the operator .loc[]# to select single rowresult = df.loc["Stuti"] # Show the dataframeresult |
Выход:
Example 2: To select multiple rows.
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Set index on a Dataframedf.set_index("Name", inplace = True) # Using the operator .loc[]# to select multiple rowsresult = df.loc[["Stuti", "Seema"]] # Show the dataframeresult |
Выход:
Пример 3: Чтобы выбрать несколько строк и определенных столбцов.
Синтаксис: Dataframe.loc [["строка1", "строка2" ...], ["столбец1", "столбец2", "столбец3" ...]]
Код:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Set "Name" column as index # on a Dataframedf.set_index("Name", inplace = True) # Using the operator .loc[] to # select multiple rows with some# particular columnsresult = df.loc[["Stuti", "Seema"], ["City", "Salary"]] # Show the dataframeresult |
Выход:
Пример 4: Выбрать все строки с некоторыми конкретными столбцами. Мы используем одиночное двоеточие [:] для выбора всех строк и списка столбцов, которые мы хотим выбрать, как показано ниже:
Синтаксис: Dataframe.loc [[:, ["столбец1", "столбец2", "столбец3"]]
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Creating a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Set "Name" column as index # on a Dataframedf.set_index("Name", inplace = True) # Using the operator .loc[] to# select all the rows with # some particular columnsresult = df.loc[:, ["City", "Salary"]] # Show the dataframeresult |
Выход:
Method 3: Using Dataframe.iloc[ ].
iloc[ ] is used for selection based on position. It is similar to loc[] indexer but it takes only integer values to make selections.
Example 1 : to select a single row.
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Using the operator .iloc[]# to select single rowresult = df.iloc[2] # Show the dataframeresult |
Выход:
Example 2: to select multiple rows.
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Create a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Using the operator .iloc[] # to select multiple rowsresult = df.iloc[[2, 3, 5]] # Show the dataframeresult |
Выход:
Example 3: to select multiple rows with some particular columns.
Code:
# import pandasimport pandas as pd # List of Tuplesemployees = [("Stuti", 28, "Varanasi", 20000), ("Saumya", 32, "Delhi", 25000), ("Aaditya", 25, "Mumbai", 40000), ("Saumya", 32, "Delhi", 35000), ("Saumya", 32, "Delhi", 30000), ("Saumya", 32, "Mumbai", 20000), ("Aaditya", 40, "Dehradun", 24000), ("Seema", 32, "Delhi", 70000) ] # Creating a DataFrame object from list df = pd.DataFrame(employees, columns =["Name", "Age", "City", "Salary"]) # Using the operator .iloc[] # to select multiple rows with# some particular columnsresult = df.iloc[[2, 3, 5], [0,
|