Python 如何为每个列名添加后缀(或前缀)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34049618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:24:01  来源:igfitidea点击:

How to add a suffix (or prefix) to each column name?

pythonpandasdataframe

提问by Klausos Klausos

I want to add _xsuffix to each column name like so:

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

featuresA = myPandasDataFrame.columns.values + '_x'

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

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

采纳答案by Stefan

You can use a listcomprehension:

你可以使用一个list理解:

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

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

也有内置的类似的方法.add_suffix(),并.add_prefix()在另一个答复中提到。

回答by Jaros?aw Szymczak

The following is the nicest way to add suffix in my opinion.

以下是我认为添加后缀的最佳方式。

df = df.add_suffix('_some_suffix')

As it is a function that is called on DataFrame and returns DataFrame - you can use it in chain of the calls.

因为它是一个在 DataFrame 上调用并返回 DataFrame 的函数 - 您可以在调用链中使用它。

回答by cs95

Elegant In-place Concatenation

优雅的就地串联

If you're trying to modify dfin-place, then the cheapest (and simplest) option is in-place addition directly on df.columns(i.e., using Index.__iadd__).

如果您尝试df就地修改,那么最便宜(也是最简单)的选项是直接在df.columns(即,使用Index.__iadd__)就地添加。

df = pd.DataFrame({"A": [9, 4, 2, 1], "B": [12, 7, 5, 4]})
df

   A   B
0  9  12
1  4   7
2  2   5
3  1   4

df.columns += '_some_suffix'
df

   A_some_suffix  B_some_suffix
0              9             12
1              4              7
2              2              5
3              1              4

To add a prefix, you would similarly use

要添加前缀,您可以类似地使用

df.columns = 'some_prefix_' + df.columns
df

   some_prefix_A  some_prefix_B
0              9             12
1              4              7
2              2              5
3              1              4


Another cheap option is using a list comprehension with f-stringformatting (available on python3.6+).

另一个便宜的选择是使用带f-string格式的列表理解(在 python3.6+ 上可用)。

df.columns = [f'{c}_some_suffix' for c in df]
df

   A_some_suffix  B_some_suffix
0              9             12
1              4              7
2              2              5
3              1              4

And for prefix, similarly,

对于前缀,类似地,

df.columns = [f'some_prefix{c}' for c in df]


Method Chaining

方法链

It is also possible to do add *fixes while method chaining. To add a suffix, use DataFrame.add_suffix

也可以在方法链接时添加 *fixes。要添加后缀,请使用DataFrame.add_suffix

df.add_suffix('_some_suffix')

   A_some_suffix  B_some_suffix
0              9             12
1              4              7
2              2              5
3              1              4

This returns a copyof the data. IOW, dfis not modified.

这将返回数据的副本。IOW,df没有修改。

Adding prefixes is also done with DataFrame.add_prefix.

添加前缀也是用DataFrame.add_prefix.

df.add_prefix('some_prefix_')

   some_prefix_A  some_prefix_B
0              9             12
1              4              7
2              2              5
3              1              4

Which also does not modify df.

这也没有修改df



Critique of add_*fix

批判 add_*fix

These are good methods if you're trying to perform method chaining:

如果您尝试执行方法链接,这些是很好的方法:

df.some_method1().some_method2().add_*fix(...)

However, add_prefix(and add_suffix) creates a copy of the entiredataframe, just to modify the headers. If you believe this is wasteful, but still want to chain, you can call pipe:

但是,add_prefix(和add_suffix)创建了整个数据帧的副本,只是为了修改标题。如果您认为这很浪费,但仍想链接,则可以调用pipe

def add_suffix(df):
    df.columns += '_some_suffix'
    return df

df.some_method1().some_method2().pipe(add_suffix)

回答by JPA

I haven't seen this solution proposed above so adding this to the list:

我还没有看到上面提出的这个解决方案,所以将其添加到列表中:

df.columns += '_x'

And you can easily adapt for the prefix scenario.

您可以轻松适应前缀场景。