如何将 pandas 数据框转换为namedtuple [英] How to convert a pandas dataframe to namedtuple
本文介绍了如何将 pandas 数据框转换为namedtuple的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将熊猫数据框转换为namedtuple?该任务将用于多处理工作.
How to convert a pandas dataframe to namedtuple? This task is going towards multiprocessing work.
def df2namedtuple(df):
return tuple(df.row)
推荐答案
itertuples
具有选项name
和index
.您可以使用它们来返回确切的输出作为您发布的函数:
itertuples
has option name
and index
. You may use them to return exact output as your posted function:
样本df:
df:
A B C D
0 32 70 39 66
1 89 30 31 80
2 21 5 74 63
list(df.itertuples(name='Row', index=False))
Out[1130]:
[Row(A=32, B=70, C=39, D=66),
Row(A=89, B=30, C=31, D=80),
Row(A=21, B=5, C=74, D=63)]
这篇关于如何将 pandas 数据框转换为namedtuple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文