目录
875、pandas.Index.drop_duplicates方法
一、用法精讲
871、pandas.Index.argmax方法
871-1、语法
# 871、pandas.Index.argmax方法
pandas.Index.argmax(axis=None, skipna=True, *args, **kwargs)
Return int position of the largest value in the Series.
If the maximum is achieved in multiple locations, the first row position is returned.
Parameters:
axis
{None}
Unused. Parameter needed for compatibility with DataFrame.
skipna
bool, default True
Exclude NA/null values when showing the result.
*args, **kwargs
Additional arguments and keywords for compatibility with NumPy.
Returns:
int
Row position of the maximum value.
871-2、参数
实际上不需要任何参数,任何额外的参数(如axis、skipna等)在Index对象的上下文中并不适用。
871-3、功能
用于返回索引中最大值的位置。
871-4、返回值
返回最大值的索引位置(整数类型)。
871-5、说明
无
871-6、用法
871-6-1、数据准备
无
871-6-2、代码示例
# 871、pandas.Index.argmax方法
import pandas as pd
# 创建一个索引
index = pd.Index([1, 2, 3, 4, 5])
# 获取最大值的位置
max_index = index.argmax()
print(max_index)
871-6-3、结果输出
# 871、pandas.Index.argmax方法
# 4
872、pandas.Index.copy方法
872-1、语法
# 872、pandas.Index.copy方法
pandas.Index.copy(name=None, deep=False)
Make a copy of this object.
Name is set on the new object.
Parameters:
name
Label, optional
Set name for new object.
deep
bool, default False
Returns:
Index
Index refer to new object which is a copy of this object.
Notes
In most cases, there should be no functional difference from using deep, but if deep is passed it will attempt to deepcopy.
872-2、参数
872-2-1、name(可选,默认值为None):指定新索引的名称,如果不提供,则新索引将没有名称。
872-2-2、deep(可选,默认值为False):布尔值,如果设置为True,则会进行深拷贝,确保索引及其数据的完全独立;如果设置为False,则只会创建一个浅拷贝。
872-3、功能
用于创建索引的副本,允许用户选择是否进行深拷贝,并可以指定新索引的名称。
872-4、返回值
返回一个新的索引对象,内容与原索引相同,但可以根据需要更改名称和拷贝方式。
872-5、说明
无
872-6、用法
872-6-1、数据准备
无
872-6-2、代码示例
# 872、pandas.Index.copy方法
import pandas as pd
# 创建一个索引
index = pd.Index([1, 2, 3, 4, 5], name='original_index')
# 创建索引的副本,指定名称
index_copy = index.copy(name='copied_index', deep=True)
print(index)
print(index_copy)
872-6-3、结果输出
# 872、pandas.Index.copy方法
# Index([1, 2, 3, 4, 5], dtype='int64', name='original_index')
# Index([1, 2, 3, 4, 5], dtype='int64', name='copied_index')
873、pandas.Index.delete方法
873-1、语法
# 873、pandas.Index.delete方法
pandas.Index.delete(loc)
Make new Index with passed location(-s) deleted.
Parameters:
loc
int or list of int
Location of item(-s) which will be deleted. Use a list of locations to delete more than one value at the same time.
Returns:
Index
Will be same type as self, except for RangeIndex.
873-2、参数
873-2-1、loc(必须):整数,表示要删除的元素的位置(索引),该位置是基于零的索引。
873-3、功能
用于从索引中删除指定位置的元素,并返回一个新的索引对象。
873-4、返回值
返回一个新的索引对象,其中指定位置的元素已被删除。
873-5、说明
无
873-6、用法
873-6-1、数据准备
无
873-6-2、代码示例
# 873、pandas.Index.delete方法
import pandas as pd
# 创建一个索引
index = pd.Index([1, 2, 3, 4, 5])
# 删除位置为2的元素(即值为3的元素)
new_index = index.delete(2)
print(index)
print(new_index)
873-6-3、结果输出
# 873、pandas.Index.delete方法
# Index([1, 2, 3, 4, 5], dtype='int64')
# Index([1, 2, 4, 5], dtype='int64')
874、pandas.Index.drop方法
874-1、语法
# 874、pandas.Index.drop方法
pandas.Index.drop(labels, errors='raise')
Make new Index with passed list of labels deleted.
Parameters:
labels
array-like or scalar
errors
{‘ignore’, ‘raise’}, default ‘raise’
If ‘ignore’, suppress error and existing labels are dropped.
Returns:
Index
Will be same type as self, except for RangeIndex.
Raises:
KeyError
If not all of the labels are found in the selected axis
874-2、参数
874-2-1、labels(必须):单个标签或标签的列表/数组,表示要从索引中删除的标签,如果提供的标签在索引中不存在,行为将取决于errors参数。
874-2-2、errors(可选,默认值为'raise'):字符串,
- '
raise
': 如果指定的标签在索引中不存在,将引发一个KeyError
。 - '
ignore
': 如果指定的标签在索引中不存在,将忽略这些标签,不会引发错误。
874-3、功能
用于从索引对象中删除一个或多个指定的标签,它不会修改原始索引,而是返回一个新的索引对象,其中不包含指定的标签。
874-4、返回值
返回一个新的pandas.Index对象,该对象是从原始索引中删除了指定标签后的结果。
874-5、说明
无
874-6、用法
874-6-1、数据准备
无
874-6-2、代码示例
# 874、pandas.Index.drop方法
import pandas as pd
# 创建一个索引对象
index = pd.Index(['a', 'b', 'c', 'd'])
# 删除标签'b'和'c'
new_index = index.drop(['b', 'c'])
print(new_index)
874-6-3、结果输出
# 874、pandas.Index.drop方法
# Index(['a', 'd'], dtype='object')
875、pandas.Index.drop_duplicates方法
875-1、语法
# 875、pandas.Index.drop_duplicates方法
pandas.Index.drop_duplicates(*, keep='first')
Return Index with duplicate values removed.
Parameters:
keep{‘first’, ‘last’, False}, default ‘first’
‘first’ : Drop duplicates except for the first occurrence.
‘last’ : Drop duplicates except for the last occurrence.
False : Drop all duplicates.
Returns:
Index
875-2、参数
875-2-1、keep(可选,默认值为'first'):字符串,
- '
first
': 保留第一次出现的重复项,删除后续的重复项。 - '
last
': 保留最后一次出现的重复项,删除之前的重复项。 False
: 删除所有重复项,不保留任何重复项。
875-3、功能
用于从索引对象中删除重复的标签,它不会修改原始索引,而是返回一个新的索引对象,其中不包含重复的标签。
875-4、返回值
返回一个新的pandas.Index对象,该对象是从原始索引中删除了重复标签后的结果。
875-5、说明
无
875-6、用法
875-6-1、数据准备
无
875-6-2、代码示例
# 875、pandas.Index.drop_duplicates方法
import pandas as pd
# 创建一个索引对象
index = pd.Index(['a', 'b', 'c', 'b', 'd', 'a'])
# 删除重复标签,保留第一次出现的
new_index_first = index.drop_duplicates(keep='first')
print(new_index_first)
# 删除重复标签,保留最后一次出现的
new_index_last = index.drop_duplicates(keep='last')
print(new_index_last)
# 删除所有重复标签
new_index_none = index.drop_duplicates(keep=False)
print(new_index_none)
875-6-3、结果输出
# 875、pandas.Index.drop_duplicates方法
# Index(['a', 'b', 'c', 'd'], dtype='object')
# Index(['c', 'b', 'd', 'a'], dtype='object')
# Index(['c', 'd'], dtype='object')