1

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!

1
  • 1
    Welcome to Stackoverflow, please read How To Ask. Pay special attention to How To Create MCVE. The more effort you'll put into posting a good question: one which is easy to read, understand and which is on topic - the chances are higher that it will attract the relevant people and you'll get help even faster. Good luck! Commented Nov 14, 2017 at 2:51

1 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

1 Comment

@Michele if this solution helped you would you accept. I'd really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.