admin管理员组文章数量:1122832
I'm looking for a way to drop columns that contain a specific word but without using loops, even if my present Dataframe doesn't have a lot of columns, I know that there is a lot of methods that avoid using loop and realize the exact same thing but incredibly faster. (as the vectorization to create new_column from existing ones)
I want to learn doing things properly.
for col in df_web.columns :
if 'post' in col and col != 'post_title':
df_web.drop(columns=col, inplace = True)
I also could use list comprehension but that still use for loop :
my_col = [col for col in df_web.columns if not col.startswith("post") or col == 'post_title']
df_web = df_web.loc[:, my_col]
Here is the original list of columns of my dataframe :
['sku', 'total_sales', 'post_author', 'post_date', 'post_date_gmt', 'product_type', 'post_title', 'post_excerpt', 'post_name', 'post_modified', 'post_modified_gmt', 'guid', 'post_type']
I'm looking for a way to drop columns that contain a specific word but without using loops, even if my present Dataframe doesn't have a lot of columns, I know that there is a lot of methods that avoid using loop and realize the exact same thing but incredibly faster. (as the vectorization to create new_column from existing ones)
I want to learn doing things properly.
for col in df_web.columns :
if 'post' in col and col != 'post_title':
df_web.drop(columns=col, inplace = True)
I also could use list comprehension but that still use for loop :
my_col = [col for col in df_web.columns if not col.startswith("post") or col == 'post_title']
df_web = df_web.loc[:, my_col]
Here is the original list of columns of my dataframe :
['sku', 'total_sales', 'post_author', 'post_date', 'post_date_gmt', 'product_type', 'post_title', 'post_excerpt', 'post_name', 'post_modified', 'post_modified_gmt', 'guid', 'post_type']
Share Improve this question edited yesterday thalback asked yesterday thalbackthalback 534 bronze badges 2- 1 read notice: minimal reproducible example – Panda Kim Commented yesterday
- Understood. As soon as I post something that contain a problem or an error to reproduce, I will also post the code to reproduce the problem itself. – thalback Commented yesterday
1 Answer
Reset to default 2You could create a mask for drop
:
m = df_web.columns.str.startswith('post_') & (df_web.columns!='post_title')
# array([ True, True, False, False, False, True, True, False, False,
# False, False, True, False])
df_web.drop(columns=df_web.columns[m], inplace=True)
Or for boolean indexing:
df_web = df_web.loc[:, (df_web.columns=='post_title')
| ~df_web.columns.str.startswith('post_')]
Or using filter
with a (nested) negative match:
df_web = df_web.filter(regex='^(?!post_(?!title))')
regex demo
Output columns:
Index(['sku', 'total_sales', 'product_type', 'post_title', 'guid'], dtype='object')
本文标签: pythonDeletion of columns that contain a specific word in the column nameStack Overflow
版权声明:本文标题:python - Deletion of columns that contain a specific word in the column name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281065a1926196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论