4

I often use this kind of line which create or replace a column and assign a value according to a condition:

df.loc[df['somecolumn'].str.endswith('_s'), 'somecolumn'] = '_sp'

I would like to do the same thing, but for the index column. My specific question is how do I refer to the index column?

df.loc[df.index.str.endswith('_s'), 'index column name?'] = '_sp'

I tried using df.index.name, but it creates a new column instead of changing the values within the index column.

4
  • You could add another line: df = df.set_index('index column name?') Commented Feb 22, 2019 at 16:00
  • df.columns return the column names, but I want to call the df.index.name without creating a new column Commented Feb 22, 2019 at 16:02
  • Ah... see my updated solution. Commented Feb 22, 2019 at 16:02
  • yes of course I could add a new line with set_index, but it will be 2 lines instead of 1... Commented Feb 22, 2019 at 16:04

3 Answers 3

5

As i told in the comment section, You don't really need to use index.str.endswith until strictly it needs to be rather use anchors like for start ^ and endswith $ that should do a Job for you.

Just taking @Scott's sample for consideration.

df.index.str.replace(r'_s$', '_sp', regex=True)

I'm retaining this answer here for the sake of posterity ..

Sign up to request clarification or add additional context in comments.

Comments

3

IIUC,

import pandas as pd
import numpy as np
​
df = pd.DataFrame(np.random.randint(0,100,(5,5)), columns=['a_s','b','c_s','d','e'], index=['A','B_s','C','D_s','E_s'])
​
df.columns = df.columns.str.replace('_s','_sp')
df.index = df.index.str.replace('_s','_sp')
​
print(df)

Output:

      a_sp   b  c_sp   d   e
A       51  80    48  93  34
B_sp    96  16    73  15  29
C       27  85    35  93  69
D_sp    92  79    90  71  85
E_sp     4  63     2  77  14

5 Comments

I want to do that with a condition like the one I wrote: if ".str.endswith('_s')"
There is no need but, you could do something like this: df.index[df.index.str.endswith('_s')].str.replace('_s','_sp')
Even better to use regex method df.columns.str.replace(r'_s$', '_sp', regex=True) with startswith ^ and for endswith $.
df.index[df.index.str.endswith('_s')].str.replace('_s','_sp') does not work because I need to do df.index = df.index[df.index.str.endswith('_s')].str.replace('_s','_sp') and there is a length mismatch
for the Scott +1 as well as i used to follow his tricks as well.
2

As suggested by pygo, this does the trick perfectly:

df.index = df.index.str.replace(r'_s$', '_sp', regex=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.