Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas Index.to_series()
函數使用索引和等於索引鍵的值創建一個Series,該索引對map有用,用於基於索引返回索引器。通過傳遞新索引標簽列表,可以為新創建的係列設置新的索引標簽。
用法: Index.to_series(index=None, name=None)
參數:
index:所得係列的索引。如果為None,則默認為原始索引
name:所得係列的名稱。如果為None,則默認為原始索引的名稱
返回:Series:dtype將基於Index值的類型。
範例1:采用Index.to_series()
函數將索引轉換為係列。
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index(['Harry', 'Mike', 'Arther', 'Nick'],
name ='Student')
# Print the Index
print(idx)
輸出:
讓我們將索引轉換為係列。
# convert the index into a series
idx.to_series()
輸出:
該函數已將索引轉換為係列。默認情況下,該函數使用原始Index的值創建了係列的index。
範例2:采用Index.to_series()
函數將索引轉換為序列,以使創建的序列使用新的索引值。
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index(['Alice', 'Bob', 'Rachel', 'Tyler', 'Louis'],
name ='Winners')
# Print the Index
print(idx)
輸出:
讓我們將索引轉換為係列。
# convert the index into a series
idx.to_series(index =['Student 1', 'Student 2', 'Student 3',
'Student 4', 'Student 5'])
輸出:
該函數已將索引轉換為係列。我們已經傳遞了一係列索引標簽,這些索引標簽將用於新創建的係列。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Timestamp.tz用法及代碼示例
- Python Pandas Series.str.contains()用法及代碼示例
- Python Pandas dataframe.std()用法及代碼示例
- Python Pandas Timestamp.dst用法及代碼示例
- Python Pandas dataframe.sem()用法及代碼示例
- Python Pandas DataFrame.ix[ ]用法及代碼示例
- Python Pandas.Categorical()用法及代碼示例
- Python Pandas.apply()用法及代碼示例
- Python Pandas TimedeltaIndex.contains用法及代碼示例
- Python Pandas Timestamp.now用法及代碼示例
- Python Pandas Series.str.pad()用法及代碼示例
- Python Pandas Series.take()用法及代碼示例
- Python Pandas dataframe.all()用法及代碼示例
- Python Pandas series.str.get()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Index.to_series()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。