Python 从数据帧条目创建元组列表

Python 从数据帧条目创建元组列表,python,pandas,list,loops,Python,Pandas,List,Loops,我有一个数据帧df: movie_title director_name ... oscar_wins oscar_nominees El Mariachi Robert Rodriguez ... 0 0 My Date with Drew Jon Gunn ... 0 0 我想做的是创建一个元组列表,其中每个元组都是数据帧的一行。因此,输出必

我有一个数据帧
df

movie_title          director_name     ...   oscar_wins   oscar_nominees
El Mariachi          Robert Rodriguez  ...       0             0
My Date with Drew    Jon Gunn          ...       0             0
我想做的是创建一个元组列表,其中每个元组都是数据帧的一行。因此,输出必须如下所示:

[(El Mariachi, Robert Rodriguez, ... , 0, 0), (My Date with Drew, Jon Gunn, ..., 0, 0) ...]
我尝试过迭代长度和列名,但没有成功

list(zip(range(len(df)), column_names)
虽然我知道为什么它不起作用,但我不知道如何实现我想要的。有没有人能帮我或给我一个解决办法


谢谢,非常感谢

只需返回列表中的值,并将内部列表映射为元组:

list(map(tuple,df.values.tolist()))
# [('El Mariachi', 'Robert Rodriguez', 0, 0), ('My Date with Drew', 'Jon Gunn', 0, 0)]

具有列表理解功能的解决方案:

L = [tuple(x) for x in df.values.tolist()]
print (L)
[('El Mariachi', 'Robert Rodriguez', 0, 0), ('My Date with Drew', 'Jon Gunn', 0, 0)]
您也可以这样做:

[tuple(x) for x in df.to_numpy()]
# [('El Mariachi', 'Robert Rodriguez', 0, 0), ('My Date with Drew', 'Jon Gunn', 0, 0)]
最快的方法是使用
记录()
: 说明:使用for循环&iterrows(),可以迭代数据帧。两元素索引(表示df的索引)和行(表示作为列表的样本行)。现在使用tuple(),将此行(列表类型对象)转换为tuple并追加到一个新列表“all_rows”

df.to_records(index=False).tolist()
 all_rows=[]
 for index, row in df.iterrows():
        all_rows.append(tuple(row))