Closed
Description
Feature 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