1

I would like to add a column to my DataFrame in which each row includes a string with the row index + 1. This is what I have attempted:

>>> import pandas as pd
>>> df = pd.DataFrame.from_records([{'some_column': 'foo'}, {'some_column': 'bar'}])
>>> df['new_column'] = f'some string {df.index + 1}'

But is just gives:

>>> df['new_column']
0    some string RangeIndex(start=1, stop=3, step=1)
1    some string RangeIndex(start=1, stop=3, step=1)

How can I achieve the desired result?

1 Answer 1

2

Use:

df.assign(new_column='some string '+(df.index+1).astype(str))
  some_column     new_column
0         foo  some string 1
1         bar  some string 2

or

df['new_column']='some string ' + (df.index+1).astype(str)

Alternative

(df.rename_axis('new_column')
   .reset_index()
   .assign(new_column=lambda x: 'some string '+ 
                                x['new_column'].add(1)
                                               .astype(str))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that works. I'm curious as to why df['new_column'] = f'some string {(df.index + 1).astype(str)}' and df['new_column'] = 'some string {}'.format((df.index + 1).astype(str)) do not work. It only seems to work using the + sting concatenation operator...
You are welcome, I think you can't pass an index object to the format function

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.