I wonder is there a method similar to add_suffix
that adds a suffix to the index of a dataframe? My current workaround looks like this.
df = pd.DataFrame({'x': [1, 2, 3]}, index=[1, 2, 3])
df = df.T.add_suffix('_x').T
# or
df.index = df.index.astype(str)+'_x'
Unfortunately, the axis
keyword is not supported by add_suffix
.
To add a string after each column label of DataFrame in Pandas, call add_suffix() method on this DataFrame, and pass the suffix string as argument to add_suffix() method.
The add_prefix() function is used to prefix labels with string prefix. For Series, the row labels are prefixed. For DataFrame, the column labels are prefixed. The string to add before each label.
To add a string before each column label of DataFrame in Pandas, call add_prefix() method on this DataFrame, and pass the prefix string as argument to add_prefix() method.
pandas.DataFrame.rename
pass a callable that gets applied to each index value
df.rename('{}_x'.format)
x
1_x 1
2_x 2
3_x 3
set_index
df.set_index(df.index.astype(str) + '_x')
x
1_x 1
2_x 2
3_x 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With