Skip to content
Advertisement

How to access particular elements of a dataframe in Pandas. Gives error

I have a dataframe df_params. It contains parameters for the stored procedure.

JavaScript

I simply want to access the elements of the dataframe in a loop:

JavaScript

But it gives me an error without really explanation:

JavaScript

The goal is to supply value to the stored procedure:

JavaScript

desired outcome from print(query):

JavaScript

Advertisement

Answer

pandas.DataFrames don’t behave exactly like numpy.ndarrays. There are basically three options:

option 1: iterrows-method: You can iterate over rows of a pandas.dataframe by

JavaScript

This is a particularly readable way, so personally I prefer this

option 2: indexing: if you want to index pandas.dataframe just like an numpy.ndarray object, go with the method .iat[]

JavaScript

This actually indexes all elements and ignores the index of the dataframe! So assuming that you have a different index (in the extreme some strings or a table with a pandas.DataTimeIndex) this still works… just as if you would have done a df_params.to_numpy()[i, 0]. Note: There exists a similar function that uses the column name: .at[]

There is a second way to index a pandas.DataFrame object and it is just a little safer with regard to columns:.loc[] It takes an index and column name(s)

JavaScript

option 3: slicing a pandas.Series object: Every column in a pandas.DataFrame is a pandas.Series object, which you can index similar (you actually index the series as described above) to a numpy.ndarray:

JavaScript

So what went wrong in your case? The double indexing is almost the same as the last example but it calls calls .loc[] under the hood and thus expects a column name and not a number (that would have been the method .iloc[]. And it is expecting to see the column first and then the row. So if you really want, you could go like this:

JavaScript

but this only works because your pandas.DataFrame has continuous numeric indices starting from 0! So please don’t do this and use the actual indices of your table (actually use one of the options above and not the last one ;) )

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement