Python Pandas Series.set_axis()

Python Pandas Series.set_axis()

Pandas系列是一个带有轴标签的一维ndarray。标签不需要是唯一的,但必须是一个可散列的类型。该对象支持基于整数和标签的索引,并提供了大量的方法来执行涉及索引的操作。

Pandas Series.set_axis()函数用于为给定的轴分配所需的索引。列或行标签的索引可以通过指定类似列表或索引来改变。

语法: Series.set_axis(labels, axis=0, inplace=None)
参数:
labels :新索引的值。
axis :要更新的轴。值为0表示行,1表示列。
inplace : 是否返回一个新的%(klass)s实例。
返回:改名为:系列

示例#1:使用Series.set_axis()函数来重置给定系列对象的轴。

# importing pandas as pd
import pandas as pd
 
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
 
# Create the Index
index_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5', 'City 6']
 
# set the index
sr.index = index_
 
# Print the series
print(sr)

输出 :

Python Pandas Series.set_axis()

现在我们将使用Series.set_axis()函数来重置给定系列对象的索引

# Create the Index
didx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W',
                     periods = 6, tz = 'Europe/Berlin')
 
 
# reset the index
sr.set_axis(didx, inplace = True)
 
# Print the series
print(sr)

输出 :

Python Pandas Series.set_axis()

正如我们在输出中看到的,Series.set_axis()函数已经成功地重置了给定的Series对象的索引。

示例#2:使用Series.set_axis()函数来重置给定系列对象的轴。

# importing pandas as pd
import pandas as pd
 
# Creating the Series
sr = pd.Series([100, 25, 32, 118, 24, 65])
 
# Print the series
print(sr)

输出 :

Python Pandas Series.set_axis()

现在我们将使用Series.set_axis()函数来重置给定系列对象的索引

# Assign the new index
sr.set_axis(['A', 'B', 'C', 'D', 'E', 'F'], inplace = True)
 
# print the series
print(sr)

输出 :

Python Pandas Series.set_axis()

正如我们在输出中看到的,Series.set_axis()函数已经成功地重置了给定的Series对象的索引。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程