Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数据帧获取列表_Python_Python 3.x_Pandas_List - Fatal编程技术网

Python 从数据帧获取列表

Python 从数据帧获取列表,python,python-3.x,pandas,list,Python,Python 3.x,Pandas,List,我想从这个数据框的列中得到一个列表(全部): 这样我就可以得到类似-->列表=(x,y,value) 我见过一些从数据帧获取列表的例子,但我不知道如何获取这样的列表 import pandas as pd df = pd.DataFrame({ "y": [5, 6, 7, 8], "x": [1, 2, 3, 4], "value": [10, 11, 12, 13] }) np_values = df[[

我想从这个数据框的列中得到一个列表(全部):

这样我就可以得到类似-->列表=(x,y,value)

我见过一些从数据帧获取列表的例子,但我不知道如何获取这样的列表

import pandas as pd

df = pd.DataFrame({
    "y": [5, 6, 7, 8],
    "x": [1, 2, 3, 4],
    "value": [10, 11, 12, 13]
})
np_values = df[["x", "y", "value"]].values
values_list = [tuple(np_values[i]) for i in range(np_values.shape[0])]
print(values_list)
印刷品

[(1,5,10)、(2,6,11)、(3,7,12)、(4,8,13)]

印刷品

[(1,5,10)、(2,6,11)、(3,7,12)、(4,8,13)]

输出:

输出:


df['x','y','Value']].应用(元组,轴=1).到列表()
df['x','y','Value'].应用(元组,轴=1).到列表()
import pandas as pd

df = pd.DataFrame({
    "y": [5, 6, 7, 8],
    "x": [1, 2, 3, 4],
    "value": [10, 11, 12, 13]
})
np_values = df[["x", "y", "value"]].values
values_list = [tuple(np_values[i]) for i in range(np_values.shape[0])]
print(values_list)
import pandas as pd
df = pd.DataFrame([[(1,1),2],[(1,1),2],[(1,1),2]],columns=['a','b'])


records = list(df.itertuples(index=False, name=None)) # get all records you can only consider required columns here
final_list = [row[0]+(row[1],) for row in records] # add all 3 elements into one tuple
print(final_list)
[(1, 1, 2), (1, 1, 2), (1, 1, 2)]