-1

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))
star_it8293
  • 399
  • 3
  • 12

2 Answers2

1

Try:

from collections import namedtuple

COLS = ['Name', 'Age']
Variable = namedtuple('Variable', field_names=COLS)
output_list = df[COLS].apply(lambda x: Variable(**x), axis=1).tolist()
print(output_list)

# Output
[Variable(Name='Harry', Age=11),
 Variable(Name='James', Age=23),
 Variable(Name='Will', Age=19)]
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

Maybe not the answer you're looking for but:

tuples = [tuple(x) for x in df1[['Name','Age']].to_numpy()]
tuples

Output:

[('Harry', 11), ('James', 23), ('Will', 19)]
Drakax
  • 1,305
  • 3
  • 9