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')
)
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.
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