How do I create a single row of data as a pandas.core.frame.Pandas object
?
So, when you iterate a dataframe (df) with for row in df.itertuples()
, each row will be a pandas.core.frame.Pandas
object. I want to create an object of that type.
For example:
import pandas as pd
d = [{'a': 1, 'b': 2}]
df = pd.DataFrame(d)
print(type(df))
print("a", df.a)
for row in df.itertuples():
print(type(row))
print("a", row.a)
myrow = None
df = pd.DataFrame(d)
for row in df.itertuples():
myrow = row
print(type(myrow))
print("a", myrow.a)
Its output is:
<class 'pandas.core.frame.DataFrame'>
a 0 1
Name: a, dtype: int64
<class 'pandas.core.frame.Pandas'>
a 1
<class 'pandas.core.frame.Pandas'>
a 1
As you can see, pandas.core.frame.DataFrame
acts differenctly as pandas.core.frame.Pandas
, as expected.
I dont really want to create myrow
the way I did above, so I was wondering whats the more efficient/direct way to create that object. Assuming I only have 1 list of data I want to convert to pandas.core.frame.Pandas
object
Every iteration of itertuples
gives an object like:
Pandas(Index=0, a=1, b=2)
Pandas
is just the default name of the itertuples
name
argument, example:
>>> help(df.itertuples)
Help on method itertuples in module pandas.core.frame:
itertuples(index=True, name='Pandas') method of pandas.core.frame.DataFrame instance
Iterate over DataFrame rows as namedtuples.
Parameters
----------
index : bool, default True
If True, return the index as the first element of the tuple.
name : str or None, default "Pandas"
The name of the returned namedtuples or None to return regular
tuples.
The default name is Pandas
. That is just the name of the namedtuple
.
If you change that name, the types would become different:
for row in df.itertuples(name='newname'):
print(row)
print(type(row))
print("a", row.a)
Output:
newname(Index=0, a=1, b=2)
<class 'pandas.core.frame.newname'>
a 1
This is the default behavior of namedtuple
s, example:
>>> from collections import namedtuple
>>> a = namedtuple('Pandas', ['x', 'y', 'z'])
>>> a(1, 2, 3)
Pandas(x=1, y=2, z=3)
>>> type(_)
<class '__main__.Pandas'>
>>>
As you can see, the type of this is Pandas
, which is the type name of this namedtuple
, so itertuples
doesn't give a Pandas
object, instead it's just the default set name of the itertuples
namedtuple
output.
As mentioned in the collections.namedtuple
documentation:
Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful
__repr__()
method which lists the tuple contents in aname=value
format.
As you can see, it creates a new type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With