python pandas renaming column name startswith

左心房为你撑大大i 提交于 2020-01-15 03:24:38

问题


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

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

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

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)  

回答1:


You can use str.replace:

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



回答2:


You can do it like this:

df = pd.DataFrame({"A":[1,2,3], "EndOfApril":[2,3,4]})
df.rename(columns = { i: "EndOfMonth" for i in  df.columns if i.startswith("EndOf") } )

outputs:

    A   EndOfMonth
0   1   2
1   2   3
2   3   4


来源:https://stackoverflow.com/questions/55301856/python-pandas-renaming-column-name-startswith

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!