0

I have this dataset:

training.head()
Out[115]: 
GridID        date      Shift  Accident  
0       1  2010-10-08      Night         0                
1       1  2011-02-16    Morning         0                     
2       1  2014-05-31      Night         0                   
3       1  2011-04-03  Afternoon         0                   
4       1  2013-02-20    Morning         0  

I would like to replace in column Shift the words "Morning", "Afternoon" and "Night" with integers 1, 2 and 3, repectively.

I tried:

training['Shift'].str.replace('Morninig','1').astype(int)

But it gives me:

 ValueError: invalid literal for int() with base 10: 'Night'
3
  • first replace all text and later change column to int using astype(int) Commented Dec 26, 2016 at 7:40
  • I tried to replace all texts but it gives me the error as shown above Commented Dec 26, 2016 at 7:43
  • 1
    it seams you didn't change all text - you still have some Night in column. Column can have only one type so astype(int) convert all elements in column. Commented Dec 26, 2016 at 7:47

2 Answers 2

3

Use Series.replace instead of str.replace, and pass it a dictionary from old value to new value. Also make sure you use inplace=True or reassign it to the series.

import pandas as pd

df = pd.DataFrame({'a': ['morning', 'afternoon']})

print(df)

>>            a
   0    morning
   1  afternoon

df['a'].replace({'morning': 1,
                 'afternoon': 2}, inplace=True)

print(df)

>>    a
   0  1
   1  2

print(df['a'].dtype)

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

Comments

1

Try this:

df2=pd.Series(data={'Night':3, 'Morning':1, 'Afternoon':2})
df.Shift = df.Shift.map(df2)

df2 = pd.read_clipboard()
df3=pd.Series(data={'Night':3, 'Morning':1, 'Afternoon':2})
df2.Shift = df2.Shift.map(df3)
df2

Output:

enter image description here

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.