pandas 将数据框转换为元组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45285244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a dataframe to list of tuples
提问by kkjoe
I have a python pandas dataframe df like this:
我有一个像这样的 python pandas 数据框 df:
a b
1 3
3 6
5 7
6 4
7 8
I want to transfer it to a list:
我想把它转移到一个列表中:
[(1,3),(3,6),(5,7),(6,4),(7,8)]
Thanks.
谢谢。
回答by cs95
If performance is important, use a list comprehension:
如果性能很重要,请使用列表理解:
[tuple(r) for r in df.to_numpy()]
# [(1, 3), (3, 6), (5, 7), (6, 4), (7, 8)]
Note: For pandas < 0.24, please use df.values
instead.
注意:对于<0.24的pandas,请df.values
改用。
You may find even better performance if you iterate over lists instead of the numpy array:
如果您迭代列表而不是 numpy 数组,您可能会发现更好的性能:
[tuple(r) for r in df.to_numpy().tolist()]
# [(1, 3), (3, 6), (5, 7), (6, 4), (7, 8)]
This method to any number of columns. However, if you want to select a specific set of columns to convert, you can select them beforehand.
此方法可用于任意数量的列。但是,如果您想选择一组特定的列进行转换,您可以预先选择它们。
[tuple(r) for r in df[['a', 'b']].to_numpy()]
# [(1, 3), (3, 6), (5, 7), (6, 4), (7, 8)]
Another alternative is using map
.
另一种选择是使用map
.
list(map(tuple, df.to_numpy()))
# [(1, 3), (3, 6), (5, 7), (6, 4), (7, 8)]
This is roughly the same as the list comprehension, performance wise. You can generalise the same way.
这与列表理解大致相同,性能方面。你可以用同样的方式概括。
Another option is to use apply
and convert the result to a list:
另一种选择是使用apply
结果并将其转换为列表:
df.apply(tuple, axis=1).tolist()
# [(1, 3), (3, 6), (5, 7), (6, 4), (7, 8)]
This is slower, so it not recommended.
这个比较慢,所以不推荐。
回答by dvitsios
You can also get the desired list like that:
您还可以像这样获得所需的列表:
zip(list(df['a']), list(df['b']))
回答by ksai
Use zip()
to create tuples
使用zip()
创建的元组
df = pd.DataFrame({'a':[1,3,5,6,7], 'b':[3,6,7,4,8]})
print(list(zip(df['a'], df['b']))