1

Using Python and Pandas, I have a

  1. dataframe, df
  2. with a column entitled 'letters'
  3. a list, letlist = ['a','b','c','d']

I would like to create a new column, 'letters_index', where the generate value would be the index of the string in column letters, in the list letlist

I tried

df['letters_index'] = letlist.index(df['letters'])

However, this didn't work. Do you have any suggestions?

0

1 Answer 1

2

As far as I understand, You need:

letlist = ['a', 'b', 'c', 'd']
print(df)

Output:

letters
0   a
1   b
2   b
3   d
4   c

And then

df['new_col'] = df['letters'].apply(lambda x: letlist.index(x))

Output:

0    0
1    1
2    1
3    3
4    2
Name: letters, dtype: int64

Beware that if the value in the column is not present in the list it would throw a ValueError.

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

1 Comment

This works great! is there anyway that we can add the output as a new column in the dataframe?

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.