ENH: Add `axis` parameter to `add_prefix` and `add_suffix`
See original GitHub issueFeature Type
-
Adding new functionality to pandas
-
Changing existing functionality in pandas
-
Removing existing functionality in pandas
Problem Description
df.add_prefix
and df.add_suffix
can be used on columns only.
Would be super handy if it can be used on the index to.
Preferably with the option inplace
.
Example:
import pandas as pd
import numpy as np
np.random.seed(123)
data = np.random.randint(0, 100, size=(4, 3))
df = pd.DataFrame(data, index=range(1, 5), columns=range(20, 23))
df.index.name = 'Quarter'
DataFrame:
20 21 22
Quarter
1 66 92 98
2 17 83 57
3 86 97 96
4 47 73 32
New feature:
df.add_prefix('Q', axis=0, inplace=True)
df.add_prefix(20, axis=1, inplace=True)
Expected result:
2020 2021 2022
Quarter
Q1 66 92 98
Q2 17 83 57
Q3 86 97 96
Q4 47 73 32
Implementation suggestion:
# columns
if df.columns is RangeIndex:
df.columns = df.columns + 2000 # df.columns stays RangeIndex
else:
df = df.add_prefix(20) # df.columns becomes list of integers
# index
if df.index is RangeIndex:
df.set_index(df.index + 2000, inplace=True)
if all(isinstance(idx, int) for idx in list(df.index)): # df.index is list of integers
df.set_index([int(str(prefix) + str(idx)) for idx in df.index], inplace=True)
else:
df.set_index([str(prefix) + str(idx) for idx in df.index], inplace=True)
Feature Description
df.add_prefix('Q', axis=0, inplace=True)
df.add_prefix(20, axis=1, inplace=True)
Alternative Solutions
Implementation suggestion:
# columns
if df.columns is RangeIndex:
df.columns = df.columns + 2000 # df.columns stays RangeIndex
else:
df = df.add_prefix(20) # df.columns becomes list of integers
# index
if df.index is RangeIndex:
df.set_index(df.index + 2000, inplace=True)
if all(isinstance(idx, int) for idx in list(df.index)): # df.index is list of integers
df.set_index([int(str(prefix) + str(idx)) for idx in df.index], inplace=True)
else:
df.set_index([str(prefix) + str(idx) for idx in df.index], inplace=True)
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Adding prefix/suffix to axis in a ssrs report - MSDN - Microsoft
I am working on a report in SSRS 2008,. In report(below) axis is from 0-80. The report has query having certain parameters and...
Read more >File Name Functions (GNU make)
The argument of the function is regarded as a series of file names, separated by whitespace. (Leading and trailing whitespace is ignored.) Each...
Read more >How to Add Suffix and Prefix to Chart Values, and Axis in the ...
How to Add Suffix and Prefix to Chart Values, and Axis in the Charts Plugin? ; Values Display Type - These only work...
Read more >Equivalent operation to addsuffix/addprefix from Make in bash
You can use shell parameter expansion on the elements of an array. ... And add suffix: ... Repeat to add both prefix and...
Read more >[Lens] Enable custom prefix and suffix for Y axis range values
Today in Time Series Visual Builder you can add custom fixed values to the Y axis range values of a chart. This allows...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dannyi96 I haven’t started working on it. You could take up this issue if you want to.
this sounds nice. Could i try working on this ? @mroeschke @jbrockmendel Since as mentioned that we are moving away from inplace, can work on the change of supporting the axis parameter.
[ PS - @still-n0thing: is this is something you would like to work on / are already working on ]