-1

Using this page from the Pandas documentation, I wanted to read a CSV into a dataframe, and then turn that dataframe into a list of named tuples.

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.itertuples.html?highlight=itertuples

I ran the code below...

import pandas as pd

def csv_to_tup_list(filename):
    myfile = filename
    df = pd.read_csv(myfile,sep=',')
    df.columns = ["term", "code"]
    tup_list = []
    for row in df.itertuples(index=False, name="Synonym"):
         tup_list.append(row)
    return (tup_list)

test = csv_to_tup_list("test.csv")
type(test[0]) 

... and the type returned is pandas.core.frame.Synonym, not named tuple. Is this how it is supposed to work, or am I doing something wrong?

My CSV data is just two columns of data:

a,1
b,2
c,3

for example.

max
  • 4,141
  • 5
  • 26
  • 55
  • What version of pandas are you on? You are linking to an older version of the documentation than the current version: 0.22.0 release date 2017-12-29. – mechanical_meat Feb 22 '18 at 17:29
  • 0.22.0 - I linked to the wrong documentation, but it hasn't changed. Thanks for the edit. – max Feb 22 '18 at 17:35

1 Answers1

1

"Named tuple" is not a type. namedtuple is a type factory. pandas.core.frame.Synonym is the type it created for this call, using the name you picked:

for row in df.itertuples(index=False, name="Synonym"):
#                                     ^^^^^^^^^^^^^^

This is expected behavior.

user2357112
  • 260,549
  • 28
  • 431
  • 505