如何为每个列名添加后缀(或前缀)? [英] How to add a suffix (or prefix) to each column name?

查看:59
本文介绍了如何为每个列名添加后缀(或前缀)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个列名中添加_x后缀,如下所示:

I want to add _x suffix to each column name like so:

featuresA = myPandasDataFrame.columns.values + '_x'

我该怎么做?另外,如果我想添加x_作为后缀,解决方案将如何更改?

How do I do this? Additionally, if I wanted to add x_ as a suffix, how would the solution change?

推荐答案

您可以使用list理解:

df.columns = [str(col) + '_x' for col in df.columns]

还有一些内置方法,例如 .add_prefix() 如另一个答案中所述.

There are also built-in methods like .add_suffix() and .add_prefix() as mentioned in another answer.

这篇关于如何为每个列名添加后缀(或前缀)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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