Reputation: 2253
I have the following dataframe:
df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]},
index=['a', 'b'])
Then:
tuples = list(df.itertuples(index=False))
tuples
Nevertheless, I noted that the tuples have the "Pandas"
name. Despite I look at the documentation for the parameters of itertuples
, I didn't found how to remove it.
[Pandas(col1=1, col2=0.10000000000000001),
Pandas(col1=2, col2=0.20000000000000001)]
I tried to put name=''
. However, it removed the col1=1
and col2=2
names, which I need. Any idea of how to remove them, in order to have something more informative like this:
[(col1=1, col2=0.10000000000000001),
(col1=2, col2=0.20000000000000001)]
Upvotes: 1
Views: 1871
Reputation: 17152
a namedtuple
needs a name... event with the collections
module you can't create a namedtuple
without a name:
import collections
collections.namedtuple('Person', 'name age gender')
Out[6]: __main__.Person
now without a name
In[7]: collections.namedtuple('', 'name age gender')
Traceback (most recent call last):
File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-7-12b9f81e7899>", line 1, in <module>
collections.namedtuple('', 'name age gender')
File "C:\Anaconda2\lib\collections.py", line 343, in namedtuple
if name[0].isdigit():
long answer short, if you want the col name, you need a namedtuple
and a namedtuple
needs a name.
if you don't want a name, then use regular tuple
but regular tuple
doesn't have 'col' argument
so calling df.itertuples()
returns namedtuple
and calling df.itertuples(index=False, name=None)
return regular tuple
Upvotes: 4
Reputation: 2011
You can try:
tuples = list(df.itertuples(index=False, name=None))
Please note the document:
name : string, default “Pandas” The name of the returned namedtuples or None to return regular tuples.
More edited: I don't think you can get print of results:
[(col1=1, col2=2)]
As documents of namedtuple states that Pandas(col1=1, col2=2)
is a namedtuple, where Pandas
is the typename
of this namedtuple. When you print it out, it will print out the typename
.
For your application, if you just try to iterate dataframe through itertuple way, both of namedtuple
and regular tuple
works, you can ignore the Panda
name.
Upvotes: 1