从数据框架创建一个名为Tuples的列表
我有一个类似的数据帧:
DF1
Name Category Age
Harry A 11
James B 23
Will A 19
我想使用new nationtuple
从collections
创建一个元组列表。列表应该是这样的:
output_list = [Variable(Name='Harry', Age=11), Variable(Name='James', Age=23), Variable(Name='Will', Age=19)]
这是我尝试使用的“ iTertuples”
output_list = list(df1[["Name","Age"]].itertuples(name='Variable', index=False))
I have a dataframe like this:
df1
Name Category Age
Harry A 11
James B 23
Will A 19
I want to create a list of tuples using namedtuple
from collections
. The list should be like this:
output_list = [Variable(Name='Harry', Age=11), Variable(Name='James', Age=23), Variable(Name='Will', Age=19)]
This is what I've tried using 'itertuples'
output_list = list(df1[["Name","Age"]].itertuples(name='Variable', index=False))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
Try:
也许不是您要寻找的答案:
输出:
Maybe not the answer you're looking for but:
Output: