Mike
Mike

Reputation: 385

retrieving column name from index (Python)

I am looping into a large dataframe with several columns to find some elements, while keeping track of the row and column position with counters.

When I find the element, I would like to find the column header corresponding to that based on the counters. Is there a simple way to do that?

For example, given:

data = [{'a': 200, 'b': 150, 'c': 200, 'd': 140, 'e':100}]
df6 = pd.DataFrame(data)

and counter=1, how do I get the corresponding column header 'b' ?

Thank you!

Upvotes: 1

Views: 2016

Answers (1)

Scott Boston
Scott Boston

Reputation: 153550

You can use the index selection from the column index of a dataframe.

data = [{'a': 200, 'b': 150, 'c': 200, 'd': 140, 'e':100}]
df6 = pd.DataFrame(data)
counter = 1
df6.columns[counter]

Output:

'b'

Upvotes: 1

Related Questions