Pandas DataFrame 基于标签的索引
索引在数据框中起着重要作用。有时我们需要为 Pandas 数据框提供基于标签的“花式索引”。为此,我们在 Pandas 中有一个名为pandas.DataFrame.lookup()的函数。 Fancy Indexing 的概念很简单,这意味着我们必须传递一个索引数组来一次访问多个数组元素。
pandas.DataFrame.lookup()函数将行和列标签的等长数组作为其属性,并返回与每个 (row, col) 对对应的值的数组。
Syntax: DataFrame.lookup(row_labels, col_labels)
Parameters:
row_labels – The row labels to use for lookup.
col_labels – The column labels to use for lookup.
Returns:
numpy.ndarray
示例 1:
Python3
# importing pandas library
import pandas as pd
# Creating a Data frame
df = pd.DataFrame([['1993', 'x', 5, 4, 7, 2],
['1994', 'v', 10, 1, 2, 0],
['1995', 'z', 2, 1, 4, 12],
['1996', 'y', 2, 1, 10, 1],
['1998', 'x', 2, 10, 40, 12],
['1999', 'x', 5, 8, 11, 6]],
columns=('Year', 'Alpha', 'x', 'y', 'z', 'v'))
# Display Data frame
df
Python3
# Use concept of fancy indexing to make new
# column 'Value' in data frame
# with help of dataframe.lookup() function
df['Value'] = df.lookup(df.index, df['Alpha'])
# Modified Data frame
df
Python3
# importing pandas library
import pandas as pd
# Creating a Data frame
df = pd.DataFrame([['1993', 'Avi', 5, 41, 70, 'Bob'],
['1994', 'Cathy', 10, 1, 22, 'Cathy'],
['1995', 'Cathy', 24, 11, 44, 'Bob'],
['1996', 'Bob', 2, 11, 10, 'Avi'],
['1998', 'Avi', 20, 10, 40, 'Avi'],
['1999', 'Avi', 50, 8, 11, 'Cathy']],
columns=('Patients', 'Name', 'Avi', 'Bob', 'Cathy', 'Aname'))
# Display Data frame
df
Python3
# Use concept of fancy indexing to make two
# new columns in data frame with help of
# dataframe.lookup() function
df['Age'] = df.lookup(df.index, df['Name'])
df['Marks'] = df.lookup(df.index, df['Aname'])
# Modified Data frame
df
Python3
# importing pandas library
import pandas as pd
# Creating a Data frame
df = pd.DataFrame([['Date1', 1850, 1992,'Avi', 5, 41, 70, 'Avi'],
['Date2', 1896, 1950, 'Cathy', 10, 1, 22, 'Avi'],
['Date2', 1900, 1920, 'Cathy', 24, 11, 44, 'Cathy'],
['Date1', 1889, 1960, 'Bob', 2, 11, 10, 'Bob'],
['Date2', 1910, 1952, 'Avi', 20, 10, 40, 'Bob'],
['Date1', 1999, 1929, 'Avi', 50, 8, 11, 'Cathy']],
columns=('Year', 'Date1', 'Date2', 'Name', 'Avi',
'Bob', 'Cathy', 'Alpha'))
# Display Data frame
df
Python3
# Use concept of fancy indexing to make two
# three columns in data frame with help of
# dataframe.lookup() function
df['Age'] = df.lookup(df.index, df['Name'])
df['Height'] = df.lookup(df.index, df['Alpha'])
df['Date_of_Birth'] = df.lookup(df.index, df['Year'])
# Modified Data frame
df
输出:
蟒蛇3
# Use concept of fancy indexing to make new
# column 'Value' in data frame
# with help of dataframe.lookup() function
df['Value'] = df.lookup(df.index, df['Alpha'])
# Modified Data frame
df
输出:
在上面的例子中,我们使用基于标签的花式索引的概念一次访问数据框的多个元素,因此使用函数dataframe.lookup()创建一个新列“值”
示例 2:
蟒蛇3
# importing pandas library
import pandas as pd
# Creating a Data frame
df = pd.DataFrame([['1993', 'Avi', 5, 41, 70, 'Bob'],
['1994', 'Cathy', 10, 1, 22, 'Cathy'],
['1995', 'Cathy', 24, 11, 44, 'Bob'],
['1996', 'Bob', 2, 11, 10, 'Avi'],
['1998', 'Avi', 20, 10, 40, 'Avi'],
['1999', 'Avi', 50, 8, 11, 'Cathy']],
columns=('Patients', 'Name', 'Avi', 'Bob', 'Cathy', 'Aname'))
# Display Data frame
df
输出:
蟒蛇3
# Use concept of fancy indexing to make two
# new columns in data frame with help of
# dataframe.lookup() function
df['Age'] = df.lookup(df.index, df['Name'])
df['Marks'] = df.lookup(df.index, df['Aname'])
# Modified Data frame
df
输出:
在上面的例子中,我们使用基于标签的花式索引的概念一次访问数据框的多个元素,因此使用函数dataframe.lookup()创建两个新列“年龄”和“标记”
示例 3:
蟒蛇3
# importing pandas library
import pandas as pd
# Creating a Data frame
df = pd.DataFrame([['Date1', 1850, 1992,'Avi', 5, 41, 70, 'Avi'],
['Date2', 1896, 1950, 'Cathy', 10, 1, 22, 'Avi'],
['Date2', 1900, 1920, 'Cathy', 24, 11, 44, 'Cathy'],
['Date1', 1889, 1960, 'Bob', 2, 11, 10, 'Bob'],
['Date2', 1910, 1952, 'Avi', 20, 10, 40, 'Bob'],
['Date1', 1999, 1929, 'Avi', 50, 8, 11, 'Cathy']],
columns=('Year', 'Date1', 'Date2', 'Name', 'Avi',
'Bob', 'Cathy', 'Alpha'))
# Display Data frame
df
输出:
蟒蛇3
# Use concept of fancy indexing to make two
# three columns in data frame with help of
# dataframe.lookup() function
df['Age'] = df.lookup(df.index, df['Name'])
df['Height'] = df.lookup(df.index, df['Alpha'])
df['Date_of_Birth'] = df.lookup(df.index, df['Year'])
# Modified Data frame
df
输出:
在上面的示例中,我们使用基于标签的花式索引的概念一次访问数据框的多个元素,因此使用函数dataframe.lookup ()创建两个新列“年龄”、“高度”和“日期_of_Birth ”
所有三个示例都展示了花式索引的工作原理以及我们如何使用花式索引和 dataframe.lookup()函数创建新列。