How to get a specific column name and index name in pandas DataFrame

I want a column name!

The average person is using DataFrame and wants a specific column or index name! Don't you think? I think well.

It's officially an API, so I think it's a bad idea, I want it. A specific column name or index name!

Well, I don't think anyone wrote it because it's probably too easy.

Sample code

Get column name

import pandas as pd
import numpy as np
a = np.array([i for i in range(100)]).reshape(10, 10)
c = 'abcdefghij'
d = 'klmnopqrst'
columns = [i for i in c]
index = [i for i in d]
df = pd.DataFrame(a, columns=columns, index=index)

So, this model is completed.

a b c d e f g h i j
k 0 1 2 3 4 5 6 7 8 9
l 10 11 12 13 14 15 16 17 18 19
m 20 21 22 23 24 25 26 27 28 29
n 30 31 32 33 34 35 36 37 38 39
o 40 41 42 43 44 45 46 47 48 49
p 50 51 52 53 54 55 56 57 58 59
q 60 61 62 63 64 65 66 67 68 69
r 70 71 72 73 74 75 76 77 78 79
s 80 81 82 83 84 85 86 87 88 89
t 90 91 92 93 94 95 96 97 98 99

This time, as a demo I would like to get the index name and column name for which df == 12.

df12 = df[df == 12]

Then

a b c d e f g h i j
k NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
l NaN NaN 12.0 NaN NaN NaN NaN NaN NaN NaN
m NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
n NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
o NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
p NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
q NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
r NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
s NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
t NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

It can be obtained.

Using this mechanism,

df12.columns[df12[df == 12].any()]

If so, the column name,

Index(['c'], dtype='object')

It can be obtained.

This will be the relevant column name.

Get index name

So how do you get the index name? If you think normally

df12.index[df12[df == 12].any()]

It seems that you can get it at.

However, this strategy does not work.

df12[df == 12].any()

As you can see by running

df12[df == 12].any() == 'c' (Column name)

Because it has become.

So

df12.index[df12[df == 12].any()]

The result of

Index(['c'], dtype='object')

It will be.

I was in trouble.

However, transposition is convenient at such times!

df12.index[df12[df == 12].T.any()]

It is a solution. Since the column name can be obtained with df12 [df == 12] .any (), All you have to do is transpose the Index name to the column name.

With this, the goal is achieved. I'm happy.

Recommended Posts

How to get a specific column name and index name in pandas DataFrame
How to reassign index in pandas dataframe
How to find a specific type (str, float etc) column in a DataFrame column
A handy function to add a column anywhere in a Pandas DataFrame
How to split and save a DataFrame
How to get a stacktrace in python
[Python] How to add rows and columns to a table (pandas DataFrame)
How to make a container name a subdomain and make it accessible in Docker
How to get a namespaced view name from a URL (path_info) in Django
How to count numbers in a specific range
How to stop a program in python until a specific date and time
[Introduction to Pandas] Read a csv file without a column name and give it a column name
How to use loc / iloc / ix to get by specifying a column in CASTable
[Pandas] How to check duplicates and delete duplicates in a table (equivalent to deleting duplicates in Excel)
How to get the variable name itself in python
How to display DataFrame as a table in Markdown
Get a participant's username and screen name in Slack
How to get RGB and HSV histograms in OpenCV
Foreigners talk: How to name classes and methods in English
Put the lists together in pandas to make a DataFrame
I tried "How to get a method decorated in Python"
How to extract other than a specific index with Numpy
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
How to get a list of built-in exceptions in python
How to get an overview of your data in Pandas
How to create dataframes and mess with elements in pandas
How to get a quadratic array of squares in a spiral!
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
How to write soberly in pandas
Get the value of a specific key up to the specified index in the dictionary list in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
How to display a specified column of files in Linux (awk)
How to get and set the NTP server name by DHCP
pandas Fetch the name of a column that contains a specific character
How to get all the possible values in a regular expression
How to get a string from a command line argument in python
How to find the memory address of a Pandas dataframe value
<Pandas> How to handle time series data in a pivot table
How to format a table using Pandas apply, pivot and swaplevel
How to get the vertex coordinates of a feature in ArcPy
I want to make the second line the column name in pandas
How to get a list excluding elements whose index is i ...?
Qiita (1) How to write a code name
Update Pandas DataFrame elements by column name
How to read CSV files in Pandas
How to use is and == in Python
How to use pandas Timestamp and date_range
How to get the date and time difference in seconds with python
How to get a value from a parameter store in lambda (using python)
How to put a half-width space before letters and numbers in Python.
How to sort by specifying a column in the Python Numpy array.
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
How to get the notebook name you are currently using in Google Colab
[Python] How to name table data and output it in csv (to_csv method)
How to get results from id in Celery
How to create a JSON file in Python
How to count the number of elements in Django and output to a template