欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

实操指南:用Python Pandas理解Index.tolist()方法

最编程 2024-07-29 15:58:30
...

Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。

Pandas Index.tolist()函数返回值列表。这些都是标量类型,这是Python标量(用于str,int,float)或pandas标量(用于Timestamp /Timedelta /Interval /Period)。

用法: Index.tolist()

参数:没有

返回:清单

范例1:采用Index.tolist()函数将索引转换为列表。

# 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 list 
idx.tolist()

输出:


范例2:采用Index.tolist()函数将索引转换为python列表。

# importing pandas as pd 
import pandas as pd 
  
# Creating the index 
idx = pd.Index(['2000-01-02', '2000-02-05', '2000-05-11', 
                            '2001-02-11', '2005-11-12']) 
  
# Print the Index 
print(idx)

输出:

让我们将索引转换为列表。

# convert the index into a list 
idx.tolist()

输出: