使用[ ]、loc和iloc在Pandas数据框架中按名称或索引选择行和列
Pandas中的索引是指从一个数据框架中选择数据的行和列。它可以选择所有的行和特定数量的列,特定数量的行和所有的列或特定数量的行和列。索引也被称为子集选择。
在Pandas中创建一个数据框架来选择行和列
一个图元的列表,例如列名是。’姓名’、’年龄’、’城市’和’工资’。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 dataframe
df
输出:
使用[]在Pandas数据框架中按名称选择列
[ ]是用来选择一个列,提到各自的列名。
示例 1:
选择一个单列。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 column
result = df["City"]
# Show the dataframe
result
输出:
示例 2:
选择多列。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 columns
result = df[["Name", "Age", "Salary"]]
# Show the dataframe
result
输出:
使用loc在Pandas数据框架中按名称选择行
.loc[] 函数通过行或列的标签来选择数据。它可以选择一个行和列的子集。有很多方法可以使用这个函数。
示例 1:
选择一个单行。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 Dataframe
df.set_index("Name", inplace = True)
# Using the operator .loc[]
# to select single row
result = df.loc["Stuti"]
# Show the dataframe
result
输出:
示例 2:
选择多行。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 Dataframe
df.set_index("Name",
inplace = True)
# Using the operator .loc[]
# to select multiple rows
result = df.loc[["Stuti", "Seema"]]
# Show the dataframe
result
输出:
示例 3:
选择多行和特定列。
**语法:** Dataframe.loc[["row1", "row2"...], ["column1", "column2", "column3"...]]
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 Dataframe
df.set_index("Name", inplace = True)
# Using the operator .loc[] to
# select multiple rows with some
# particular columns
result = df.loc[["Stuti", "Seema"],
["City", "Salary"]]
# Show the dataframe
result
输出:
示例 4:
选择所有具有某些特定列的行。我们用一个冒号[ :]来选择所有的行和我们要选择的列的列表,如下所示。
**语法:** Dataframe.loc[[:, ["column1", "column2", "column3"]]
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 Dataframe
df.set_index("Name", inplace = True)
# Using the operator .loc[] to
# select all the rows with
# some particular columns
result = df.loc[:, ["City", "Salary"]]
# Show the dataframe
result
输出:
使用iloc在Pandas数据框架中按索引选择行
iloc[ ] 用于基于位置的选择。它与loc[]索引器类似,但它只取整数值来进行选择。
示例 1:
选择单一行。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 row
result = df.iloc[2]
# Show the dataframe
result
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 row
result = df.iloc[2]
# Show the dataframe
result
输出:
示例 2:
选择多行。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 rows
result = df.iloc[[2, 3, 5]]
# Show the dataframe
result
输出:
示例 3:
选择具有某些特定列的多行。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 columns
result = df.iloc[[2, 3, 5],
[0, 1]]
# Show the dataframe
result
输出:
示例 4:
选择所有具有某些特定列的行。
# import pandas
import pandas as pd
# List of Tuples
employees = [('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 all the rows with
# some particular columns
result = df.iloc[:, [0, 1]]
# Show the dataframe
result
输出: