python pandas重命名列名开头为 [英] python pandas renaming column name startswith

查看:304
本文介绍了python pandas重命名列名开头为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个具有统一列名的Excel文件,除了一个.

i have multiple excel files with uniform column names, except for one.

一个文件称为EndOfMarchStatus,另一个文件称为EndofAprilStatus,依此类推.

One file calls it EndOfMarchStatus, another file calls it EndofAprilStatus, and so on.

我需要将列名称更改为只说EndofMonthStatus.真的没有答案,我可以找到匹配这个问题的答案.

i need to change the column name to just say EndofMonthStatus. there really is no answer i could find that matches this question.

使用通配符或startswith的某种形式的重命名命令可能会起作用.

some form of rename command with wildcards or startswith will probably work.

我尝试过但没有用的东西是:

things i've tried but did not work are:

sheet1df.columns.str.replace('Endof.*', 'EndOfMonthStatus')

sheet1df.rename(columns={sheet1df.filter(regex='*.Status').columns[0]: 'EndOfMonthStatus'}, inplace=True)

sheet1df.rename(columns={'^Status':'EndOfMonthStatus'}, inplace=True)

sheet1df.rename(columns=lambda x: x.replace('Endof%', 'EndOfMonthStatus'), inplace=True)  

推荐答案

您可以使用str.replace:

df.columns = df.columns.str.replace('(?<=EndOf)(\w+)(?=Status)', 'Month')

这篇关于python pandas重命名列名开头为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆