Krishna
Krishna

Reputation: 1382

Get data from Pandas DataFrame using column values

>>> df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]},
...                   index=['dog', 'hawk'])
>>> df
      num_legs  num_wings
dog          4          0
hawk         2          2
>>> for row in df.itertuples():
...     print(row)
...
Pandas(Index='dog', num_legs=4, num_wings=0)
Pandas(Index='hawk', num_legs=2, num_wings=2)

I am parsing an excel sheet using pandas.DataFrame.itertuples which will give me a pandas.DataFrame over each iteration. Consider the pandas.DataFrame returned in each iteration as shown above.

Now off the each data frame Pandas(Index='dog', num_legs=4, num_wings=0) I would like to access the values using the keyword num_legs however upon using the same I get the below exception.

TypeError: tuple indices must be integers, not str

Could someone help on how to retrieve the data from the data frames using the column headers directly.

Upvotes: 2

Views: 2164

Answers (3)

Mohit Musaddi
Mohit Musaddi

Reputation: 143

I faced the same error when using a variable.

v = 'num_legs'
for row in df.itertuples():
    print(row[v])

TypeError: tuple indices must be integers or slices, not str

To use df.itertuples() and use the attribute name as a variable.

v = 'num_legs'
for row in df.itertuples():
    print(getattr(row, v))

At the end df.itertuples() is faster than df.iterrows().

Upvotes: 4

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

you could use iterrows(),

for u,row in df.iterrows():
    print(u)
    print (row)
    print (row['num_legs'])

O/P:

dog
num_legs     4
num_wings    0
Name: dog, dtype: int64
4
hawk
num_legs     2
num_wings    2
Name: hawk, dtype: int64
2

Upvotes: 1

meW
meW

Reputation: 3967

Here:

for row in df.itertuples():
    print(row.num_legs)
  # print(row.num_wings)   # Other column values

# Output
4
2

Upvotes: 1

Related Questions