pandas Fetch the name of a column that contains a specific character

pandas Fetch the name of a column that contains a specific character

In this article pandas 0.19.I am using 2.

Introduction


For data type conversion, organizing variables for analysis, etc. I think that there are surprisingly many cases where you want to get the column name in a way that suits your needs.

I think there are various methods, but here I use find.

data


# coding:utf-8

df = pd.DataFrame(
    {'id':['1001','1002','1003','1004'],
      'x01':[3,2,3,1],
      'x02':[1,2,1,1],
      'y01':[3,2,2,2],
      'y02':[1,1,1,2],
      'z01':[1,2,3,3],
     })

df
id x01 x02 y01 y02 z01
0 1001 3 1 3 1 1
1 1002 2 2 2 1 2
2 1003 3 1 2 1 3
3 1004 1 1 2 2 3

Use find to get column names containing specific characters


Use list comprehension and find to get what meets your criteria. The function find returns the position where the character first appears. If not, -1 is returned. Here we want to fetch a variable containing'y'.

temp_col = [item for item in df.columns if item.find('y') != -1]

print temp_col
['y01', 'y02']

You can also use OR to:

temp_col_2 = [item for item in df.columns if item.find('y') != -1 or item.find('z') != -1]

print temp_col_2
['y01', 'y02', 'z01']

You can use the obtained list to get the data narrowed down to a specific column as shown below.

df[['id'] + temp_col]
id y01 y02
0 1001 3 1
1 1002 2 2
2 1003 3 1
3 1004 1 1

Recommended Posts

pandas Fetch the name of a column that contains a specific character
Get the variable name of the variable as a character string.
# Function that returns the character code of a string
[Python] Extracts data frames that do not match a specific column with other data frames of Pandas
How to get a specific column name and index name in pandas DataFrame
A collection of Numpy, Pandas Tips that are often used in the field
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
A story that reduces the effort of operation / maintenance
[Python] A program that counts the number of valleys
A regular expression that finds N or more consecutive substrings of the same character
[Ansible] Example of playbook that adds a character string to the first line of the file
Make a BOT that shortens the URL of Discord
Make a note of the list of basic Pandas usage
[Python] Leave only the elements that start with a specific character string in the array
When reading a csv file with read_csv of pandas, the first column becomes index
Generate that shape of the bottom of a PET bottle
A story about changing the master name of BlueZ
A story that analyzed the delivery of Nico Nama.
[Python] A program that compares the positions of kangaroos.
Calculate the product of matrices with a character expression?
I made a twitter app that identifies and saves the image of a specific character on the twitter timeline by pytorch transfer learning
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
A tool that automatically turns the gacha of a social game
A program that removes specific characters from the entered text
Get the number of specific elements in a python list
Decorator that displays "FIN method name" at the end of the method
Display the signal strength RSSI of a specific SSID (mac)
A Python script that compares the contents of two directories
The Power of Pandas: Python
The story of creating a bot that displays active members in a specific channel of slack with python
A memo that reproduces the slide show (gadget) of Windows 7 on Windows 10.
[python, ruby] fetch the contents of a web page with selenium-webdriver
A formula that simply calculates the age from the date of birth
A story that struggled to handle the Python package of PocketSphinx
A function that measures the processing time of a method in python
How to find the memory address of a Pandas dataframe value
The story of creating a site that lists the release dates of books
I want to make the second line the column name in pandas
I made a slack bot that notifies me of the temperature
[Golang] Check if a specific character string is included in the character string
[python] A note that started to understand the behavior of matplotlib.pyplot
The story of making a module that skips mail with python
[Python] A program that rotates the contents of the list to the left
A function that returns a random name
The story of writing a program
Sort the list that contains the dict
Display the signal strength RSSI of a specific SSID (raspberry pi (linux))
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
[Python] A program that calculates the number of chocolate segments that meet the conditions
I made a calendar that automatically updates the distribution schedule of Vtuber
[Python] A program that calculates the number of socks to be paired
A memorandum of how to write pandas that I tend to forget personally
I wrote a corpus reader that reads the results of MeCab analysis
The story of developing a web application that automatically generates catchphrases [MeCab]
How to create a wrapper that preserves the signature of the function to wrap
A note about the functions of the Linux standard library that handles time
The story of making a package that speeds up the operation of Juman (Juman ++) & KNP
Obtain the direct product of sequences of indefinite length within a specific sequence.
Find out the name of the method that called it from the method that is python
I made a mistake in fetching the hierarchy with MultiIndex of pandas