gpt4 book ai didi

python - 将数据框的列附加到列表中

转载 作者:行者123 更新时间:2023-12-04 08:42:12 25 4
gpt4 key购买 nike

我有一个数据框:

col1   col2  col3
1 7 8
1.5 6.7 9
1.24 5.5 8.8
我想编写一个函数,它将这些列值作为列表添加到列表中。所以会有一个嵌套列表,其中包含这些值:
[[1,1.5,1.24], [7,6.7,5.5], [8,9,8.8]]
我写了这个函数,但它不起作用:
def func1(df):
my_list=[]
for i in df:
my_list.append(i)

最佳答案

这是解决方案。您只需将数据框名称更改为“df”:

lst=[]
for i in range(len(df.columns)):
takelists = df.iloc[:, i].tolist()
lst.append(takelists)

print(last)
例子:
  Column Column2 Column3
0 1 4 7
1 2 5 8
2 3 6 9

Out[129]:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

关于python - 将数据框的列附加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64507877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com