pandas 中的 tolist() 和 to_list()

  • 在使用pandas的时候,有时候会需要将pandas中的数据类型转换为python中的list,而pandas也提供了tolist()to_list()这两个方法来实现这一需求

  • 几乎可以认为pandas中的tolist()to_list()用法没有差别

  • 还顺便介绍了numpy中的tolist()方法,其主要特点是可以作用于任意维度的数组

1. tolist()

复制代码
	pandas.api.extensions.ExtensionArray.tolist()
		
		Return a list of the values.
 		
 		These are each a scalar type, which is a Python scalar (for str, int, float) or 
 		a pandas scalar (for Timestamp/Timedelta/Interval/Period)
python 复制代码
>>> arr = pd.array([1, 2, 3])
>>> arr.tolist()
[1, 2, 3]

这是官方文档上对于tolist()的说明与示例。从中可以看出:

  • 该方法属于pandas扩展的,从其所属的pandas.api.extensions.ExtensionArray即可看出
  • 该方法返回一个listlist中元素的类型既可以为python的数据类型,也可以pandas中的类型,(在较早的版本中,返回列表中的元素类型为numpy类型或者pandas类型)

下面用示例来介绍tolist()方法的使用

python 复制代码
df = pd.DataFrame(
    {"A": [1, 2, 3], "B": [4, 5, 6]},
    index=["x", "y", "z"]
)
python 复制代码
   A  B
x  1  4
y  2  5
z  3  6

1.1. 不能直接用于DataFrame

python 复制代码
df.tolist()
# AttributeError: 'DataFrame' object has no attribute 'tolist'

1.2. 用于indexcolumn属性上

python 复制代码
index_tolist = df.index.tolist()
print(index_tolist)
print(type(index_tolist))
print(type(index_tolist[0]))

# ['x', 'y', 'z']
# <class 'list'>
# <class 'str'>
python 复制代码
columns_tolist = df.columns.tolist()
print(columns_tolist)
print(type(columns_tolist))
print(type(columns_tolist[0]))

# ['A', 'B']
# <class 'list'>
# <class 'str'>

1.3. 用于行列数据上

python 复制代码
row_tolist = df.iloc[0].tolist()
print(row_tolist)
print(type(row_tolist))
print(type(row_tolist[0]))

# [1, 4]
# <class 'list'>
# <class 'int'>
python 复制代码
col_tolist = df["A"].tolist()
print(col_tolist)
print(type(col_tolist))
print(type(col_tolist[0]))

# [1, 2, 3]
# <class 'list'>
# <class 'int'>

此处也表明tolist()Series()的用法

1.4. 用在多维索引上

python 复制代码
index_df = pd.DataFrame(
    [["bar", "one"], ["bar", "two"], ["foo", "one"], ["foo", "two"]],
    columns=["first", "second"],
)

mul_index = pd.MultiIndex.from_frame(index_df)
mul_df = pd.DataFrame(np.random.randn(4, 3), index=mul_index)
python 复制代码
                     0         1         2
first second                              
bar   one    -0.625643  0.533483  0.066657
      two    -1.759180  1.116185  0.264087
foo   one    -0.773947 -1.649559  1.865090
      two     1.200301 -3.090575 -1.464554
python 复制代码
mul_index_tolist = mul_df.index.tolist()
print(mul_index_tolist)
print(type(mul_index_tolist))
print(type(mul_index_tolist[0]))
print(type(mul_index_tolist[0][0]))

# [('bar', 'one'), ('bar', 'two'), ('foo', 'one'), ('foo', 'two')]
# <class 'list'>
# <class 'tuple'>
# <class 'str'>

2. to_list()

复制代码
	pandas.Index.to_list()
	pandas.Series.to_list()
		
		Return a list of the values.
 		
 		These are each a scalar type, which is a Python scalar (for str, int, float) or 
 		a pandas scalar (for Timestamp/Timedelta/Interval/Period)

从官方文档可以看出,to_list()tolist() 的解释说明完全一致,所不同的是tolist()属于pandas扩展方法,而to_list()则属于IndexSeries类型的方法。

2.1. 不能直接用于DataFrame

python 复制代码
df.to_list()
# AttributeError: 'DataFrame' object has no attribute 'to_list'

2.2. 用于indexcolumn属性上

python 复制代码
index_to_list = df.index.to_list()
print(index_to_list)
print(type(index_to_list))
print(type(index_to_list[0]))

# ['x', 'y', 'z']
# <class 'list'>
# <class 'str'>
python 复制代码
columns_to_list = df.columns.to_list()
print(columns_to_list)
print(type(columns_to_list))
print(type(columns_to_list[0]))

# ['A', 'B']
# <class 'list'>
# <class 'str'>

2.3. 用于行列数据上

python 复制代码
row_to_list = df.iloc[0].to_list()
print(row_to_list)
print(type(row_to_list))
print(type(row_to_list[0]))

# [1, 4]
# <class 'list'>
# <class 'int'>
python 复制代码
col_to_list = df["A"].to_list()
print(col_to_list)
print(type(col_to_list))
print(type(col_to_list[0]))

# [1, 2, 3]
# <class 'list'>
# <class 'int'>

此处也表明to_list()Series()上的用法

2.4. 用在多维索引上

python 复制代码
index_df = pd.DataFrame(
    [["bar", "one"], ["bar", "two"], ["foo", "one"], ["foo", "two"]],
    columns=["first", "second"],
)

mul_index = pd.MultiIndex.from_frame(index_df)
mul_df = pd.DataFrame(np.random.randn(4, 3), index=mul_index)
python 复制代码
                     0         1         2
first second                              
bar   one    -0.625643  0.533483  0.066657
      two    -1.759180  1.116185  0.264087
foo   one    -0.773947 -1.649559  1.865090
      two     1.200301 -3.090575 -1.464554
python 复制代码
mul_index_to_list = mul_df.index.to_list()

print(mul_index_to_list)
print(type(mul_index_to_list))
print(type(mul_index_to_list[0]))
print(type(mul_index_to_list[0][0]))

# [('bar', 'one'), ('bar', 'two'), ('foo', 'one'), ('foo', 'two')]
# <class 'list'>
# <class 'tuple'>
# <class 'str'>

3. numpy 中的 tolist()

复制代码
numpy.ndarray.tolist()

	Return the array as an a.ndim-levels deep nested list of Python scalars.

	Return a copy of the array data as a (nested) Python list. 
	Data items are converted to the nearest compatible builtin Python type, via the item function.
	If a.ndim is 0, then since the depth of the nested list is 0, it will not be a list at all, but a simple Python scalar.

numpy 中的 tolist() 着重强调了两点:

  • 列表中的元素类型都是python数据类型
  • 可以对0维、1维和2维以及更高维度的numpy.ndarray进行转换,这一点是pandas中的tolistto_list所不具备的
python 复制代码
a = np.uint32([1, 2])
a_list = list(a)
a_list    # [1, 2]
type(a_list[0])   # <class 'numpy.uint32'>

a_tolist = a.tolist()
a_tolist   # [1, 2]
type(a_tolist[0])  # <class 'int'>
python 复制代码
a = np.array([[1, 2], [3, 4]])
list(a)   # [array([1, 2]), array([3, 4])]
a.tolist()   # [[1, 2], [3, 4]]
python 复制代码
a = np.array(1)
# list(a)
# Traceback (most recent call last):
#  ...
# TypeError: iteration over a 0-d array
a.tolist()    # 1
相关推荐
好易学·数据结构8 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
Ashlee_code14 小时前
裂变时刻:全球关税重构下的券商交易系统跃迁路线图(2025-2027)
java·大数据·数据结构·python·云原生·区块链·perl
闻缺陷则喜何志丹15 小时前
【带权的并集查找】 P9235 [蓝桥杯 2023 省 A] 网络稳定性|省选-
数据结构·c++·蓝桥杯·洛谷·并集查找
jie*15 小时前
python(one day)——春水碧于天,画船听雨眠。
开发语言·数据结构·python·算法·线性回归
草莓熊Lotso17 小时前
【LeetCode刷题指南】--数组串联,合并两个有序数组,删除有序数组中的重复项
c语言·数据结构·其他·刷题
weixin_4196583117 小时前
数据结构之B-树
java·数据结构·b树
H_HX_xL_L17 小时前
数据结构的算法分析与线性表<1>
数据结构·算法
overFitBrain17 小时前
数据结构-2(链表)
数据结构
xienda17 小时前
数据结构排序算法总结(C语言实现)
数据结构·算法·排序算法
科大饭桶17 小时前
数据结构自学Day8: 堆的排序以及TopK问题
数据结构·c++·算法·leetcode·二叉树·c