Extract specific multiple columns with pandas

TL;DR

By using str accessor, you can easily create a mask with column specifications by partial matching. (Example: df.columns.str.contains ('arbitrary column name'))

Extract multiple columns at once

The point is

--Refer to columns and refer to the character string contained in it. ――The final result will be a list containing booleans with or without columns.

So if you want to use it

#In this case, take out any multiple columns
include_list = df.columns[df.columns.str.contains('hoge_') * df.columns.str.contains('fuga_')]
df_prep = df[include_list]
#In this case, extract other than arbitrary multiple columns
# Point:tilde(~)The mask is inverted by using
exclude_list = df.columns[~df.columns.str.contains('hoge_') * ~df.columns.str.contains('fuga_')]
df_prep = df[exclude_list]

There are also options, case (case sensitive) and regex (use of regular expression patterns), so you can use them flexibly.

application of str accessor

This time, I did it for the column, but you can do the same when you want to extract an arbitrary character string from a specific column, for example.

df['user'].str.contains('Ruri')

Recommended Posts

Extract specific multiple columns with pandas
Extract the maximum value with pandas.
Extract multiple elements with Numpy array
Load csv with duplicate columns in pandas
Tips for plotting multiple lines with pandas
Extract lines containing a specific "string" in Pandas
Update multiple tables at once with pandas to_sql
Type conversion of multiple columns of pandas DataFrame with astype at the same time
Multiple selections with Jupyter
Quickly visualize with Pandas
Bootstrap sampling with Pandas
Processing datasets with pandas (2)
Merge datasets with pandas
Extract N samples for each group with Pandas DataFrame
Get the sum of each of multiple columns with awk
Learn Pandas with Cheminformatics
Extract elements other than a specific index with Numpy
[Python] Read Japanese csv with pandas without garbled characters (and extract columns written in Japanese)
How to extract null values and non-null values with pandas
How to extract non-missing value nan data with pandas
Data visualization with pandas
Data manipulation with Pandas!
Shuffle data with pandas
Extract the maximum value with pandas and change that value
Extract EXIF with sips
How to extract non-missing value nan data with pandas
Calculate the time difference between two columns with Pandas DataFrame
How to extract other than a specific index with Numpy
Extract non-numeric elements with pandas.DataFrame
Read csv with python pandas
Load nested json with pandas
Draw multiple graphs using Pandas
Multiple regression analysis with Keras
Extract Twitter data with CSV
Extract specific languages from Wiktionary
[Python] Change dtype with pandas
Browse columns encrypted with sqlalchemy
Extract numbers with regular expressions
Standardize by group with pandas
Animate multiple graphs with matplotlib
Swap columns in pandas dataframes
Control multiple robots with jupyter-lab
Extract peak values with scipy
Prevent omissions with pandas print
Data processing tips with Pandas
Two methods of conditional extraction with pandas (single condition, multiple conditions)