Retrieve name of column from its Index in Pandas

2024/11/20 14:40:00

I have a pandas dataframe and a numpy array of values of that dataframe. I have the index of a specific column and I already have the row index of an important value. Now I need to get the column name of that particular value from my dataframe.

After searching through the documentations, I found out that I can do the opposite but not what I want.

Answer

I think you need index columns names by position (python counts from 0, so for fourth column need 3):

colname = df.columns[pos]

Sample:

df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9],'D':[1,3,5],'E':[5,3,6],'F':[7,4,3]})print (df)A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3pos = 3
colname = df.columns[pos]
print (colname)
D

pos = [3,5]
colname = df.columns[pos]
print (colname)
Index(['D', 'F'], dtype='object')
https://en.xdnf.cn/q/26310.html

Related Q&A

Purpose of return self python

I have a problem with return selfclass Fib: def __init__(self, max):self.max = maxdef __iter__(self): self.a = 0self.b = 1return selfdef __next__(self):fib = self.aif fib > self.max:raise StopIterat…

tempfile.TemporaryDirectory context manager in Python 2.7

Is there a way to create a temporary directory in a context manager with Python 2.7?with tempfile.TemporaryDirectory() as temp_dir:# modify files in this dir# here the temporary diretory does not exis…

Matplotlib returning a plot object

I have a function that wraps pyplot.plt so I can quickly create graphs with oft-used defaults:def plot_signal(time, signal, title=, xlab=, ylab=,line_width=1, alpha=1, color=k,subplots=False, show_grid…

Where is the history file for ipython

I can not determine where the ipython is storing its history.a. There is no ~/.pythonhistory:12:49:00/dashboards $ll ~/.py* ls: /Users/steve/.py*: No such file or directoryb. Nothing special in the pyt…

How do I find what is using memory in a Python process in a production system?

My production system occasionally exhibits a memory leak I have not been able to reproduce in a development environment. Ive used a Python memory profiler (specifically, Heapy) with some success in th…

In Django is there a way to display choices as checkboxes?

In the admin interface and newforms there is the brilliant helper of being able to define choices. You can use code like this:APPROVAL_CHOICES = ((yes, Yes),(no, No),(cancelled, Cancelled), )client_app…

How to get the first 2 letters of a string in Python?

Lets say I have a string str1 = "TN 81 NZ 0025" two = first2(str1) print(two) # -> TNHow do I get the first two letters of this string? I need the first2 function for this.

Python sqlite3 version

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >…

Python debugger (pdb) stopped handlying up/down arrows, shows ^[[A instead

I am using python 2.6 in a virtualenv on an Ubuntu Linux 11.04 (natty) machine. I have this code in my (django) python code:import pdb ; pdb.set_trace()in order to launch the python debugger (pdb).Up u…

Un-persisting all dataframes in (py)spark

I am a spark application with several points where I would like to persist the current state. This is usually after a large step, or caching a state that I would like to use multiple times. It appears …