Skip to content
Advertisement

Accessing pandas cell value using df.itertuples() and column name gives AttributeError

I have the following dataframe from where I want to retrieve the cell values using index and column names. The left column indicates the index values whereas the column names are from 1 to 5. This is a dummy dataframe which looks small but going forward I will be using this code to access a dataframe with 100+ columns and it is not possible to know the column names beforehand.

1 2 3 4 5
t_1 1 0 0 0 1
t_2 1 1 0 0 0
t_3 1 0 0 0 0
t_4 1 0 1 0 1

To retrieve the values from this dataframe I am using the itertuples() to loop over the pandas dataframe. Please note that using iterrows() this can be easily done but it is much slower because of which I want to avoid using that. Here is the code snippet to iterate the dataframe:

for row in input_df.itertuples():
    print(row.Index)
    for col in input_df.columns[1:]:
        print(row.col)

Since I won’t be knowing the column names beforehand I want to get the column names from the dataframe list and then use it to fetch the cell values. For example, row t_1 column 1 should return 1. However, with the above code I am getting the following error:

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

If I mention the exact column name in place of col with row then I am getting the result without any error. Please help me understand what am I doing wrong here to get this error. Is there any other solution apart from iterrows() to get the cell value with column names?

Advertisement

Answer

Simply change row.col to getattr(row, col):

for row in input_df.itertuples():
    print(row.Index)
    for col in input_df.columns[1:]:
        print(getattr(row, col))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement