Python|熊猫系列.set_axis()
Pandas 系列是带有轴标签的一维 ndarray。标签不必是唯一的,但必须是可散列的类型。该对象支持基于整数和基于标签的索引,并提供了许多用于执行涉及索引的操作的方法。
Pandas Series.set_axis()函数用于为给定轴分配所需的索引。可以通过分配类似列表或索引来更改列或行标签的索引。
Syntax: Series.set_axis(labels, axis=0, inplace=None)
Parameter :
labels : The values for the new index.
axis : The axis to update. The value 0 identifies the rows, and 1 identifies the columns.
inplace : Whether to return a new %(klass)s instance.
Returns : renamed : series
示例 #1:使用 Series.set_axis()函数重置给定 Series 对象的轴。
Python3
# 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)
Python3
# 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)
Python3
# 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)
Python3
# Assign the new index
sr.set_axis(['A', 'B', 'C', 'D', 'E', 'F'], inplace = True)
# print the series
print(sr)
输出 :
现在我们将使用 Series.set_axis()函数来重置给定系列对象的索引
Python3
# 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)
输出 :
正如我们在输出中看到的,Series.set_axis()函数已成功重置给定 Series 对象的索引。
示例 #2:使用 Series.set_axis()函数重置给定 Series 对象的轴。
Python3
# 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)
输出 :
现在我们将使用 Series.set_axis()函数来重置给定系列对象的索引
Python3
# Assign the new index
sr.set_axis(['A', 'B', 'C', 'D', 'E', 'F'], inplace = True)
# print the series
print(sr)
输出 :
正如我们在输出中看到的,Series.set_axis()函数已成功重置给定 Series 对象的索引。