pandas.DataFrame.add_prefix#

DataFrame.add_prefix(prefix)[源代码]#

使用字符串为标签添加前缀 prefix

对于系列,行标签带有前缀。对于DataFrame,列标签带有前缀。

参数
prefix应力

要添加到每个标签之前的字符串。

退货
系列或DataFrame

具有更新标签的新系列或DataFrame。

参见

Series.add_suffix

使用字符串作为行标签的后缀 suffix

DataFrame.add_suffix

使用字符串作为列标签的后缀 suffix

示例

>>> s = pd.Series([1, 2, 3, 4])
>>> s
0    1
1    2
2    3
3    4
dtype: int64
>>> s.add_prefix('item_')
item_0    1
item_1    2
item_2    3
item_3    4
dtype: int64
>>> df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [3, 4, 5, 6]})
>>> df
   A  B
0  1  3
1  2  4
2  3  5
3  4  6
>>> df.add_prefix('col_')
     col_A  col_B
0       1       3
1       2       4
2       3       5
3       4       6