Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas get from pandas.core.frame.Pandas object

Tags:

pandas

I have a pandas.core.frame.Pandas object and need to return a value from it based on its attribute name. The below code works

val = pandas_object.attr_name

However, the attribute I need to get changes, so I need something dynamic like

val = pandas_object.get(attr_name)

The pandas objecet does not have a getter function as I get the below error

AttributeError: 'Pandas' object has no attribute 'get'

Other pandas classes seem to have that. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.get.html

I cant find much documentation on this type oaf pandas object. Is there a way to get the values similar to .get()?

This is the object I have

Pandas(Index=0, Time_USec=Timestamp('2016-12-19 09:05:00.683243'), OrderId=0, Status='New')

When I follow the adive to use getattr I get the below error

print event.__getattr__("Time_USec")

AttributeError: 'Pandas' object has no attribute 'getattr'

like image 430
chrise Avatar asked Jan 28 '17 05:01

chrise


People also ask

What is a pandas core frame DataFrame?

What is a DataFrame? A Pandas DataFrame is a 2 dimensional data structure, like a 2 dimensional array, or a table with rows and columns.

How do you access DataFrame elements in Python?

By using at and iat attributes We can also access a single value of a DataFrame with the help of “at” and “iat” attributes. Access a single value by row/column name. At and iat take two arguments. If we pass only one argument, then it will generate an error.

How do you retrieve data from a DataFrame in Python?

The get() method returns the specified column(s) from the DataFrame. If you specify only one column, the return value is a Pandas Series object. To specify more than one column, specify the columns inside an array. The result will be a new DataFrame object.

What is Tolist () in pandas?

Pandas series can be converted to a list using tolist() or type casting method. There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations.


2 Answers

Another solution I stumbled across is

pandas_object.__getattribute__(attr_name)

Seems to be more natural/intuitive than the

pandas_object.__dict__[attr_name]

Unfortunately, as opposed to .get this method allows not to set a default value

like image 159
chrise Avatar answered Sep 17 '22 17:09

chrise


The Pandas class you mention is actually a dynamically created named tuple class (see here). By default the class is named "Pandas" but you can actually name it whatever you want like so:

my_tuples = list(df.itertuples(name='Whatever'))
assert isinstance(my_tuples[0], tuple)
assert my_tuples[0].__class__.__name__ == 'Whatever'

As you can see from the official documentation for named tuples, you can convert them to dictionaries like so:

some_object = some_namedtuple._asdict()
val = some_object.get(attr_name)

But a more Pythonic way of retrieving an attribute would be to just simply use the built-in getattr function which also works with any other Python object:

val = getattr(some_namedtuple, attr_name)
like image 29
Mario Abarca Avatar answered Sep 17 '22 17:09

Mario Abarca