Home > Back-end >  Create a list of namedtuples from a dataframe
Create a list of namedtuples from a dataframe

Time:06-02

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))

CodePudding user response:

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)]

CodePudding user response:

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)]
  • Related