I am looking for the most efficient way to convert a pandas
DataFrame
into a list of typed NamedTuple
- below is a simple example with the expected output.
I would like to get the correct type conversion aligned with the type defined in the dataframe.
from typing import NamedTuple
import pandas as pd
if __name__ == "__main__":
data = [["tom", 10], ["nick", 15], ["juli", 14]]
People = pd.DataFrame(data, columns=["Name", "Age"])
Person = NamedTuple("Person", [("name", str), ("age", int)])
# ...
# ...
# expected output
# [Person(name='tom', age=10), Person(name='nick', age=15), Person(name='juli', age=14)]