Claudio
Claudio

Reputation: 63

Convert column object into integer in python

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'

Upvotes: 0

Views: 1127

Answers (2)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17064

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

Upvotes: 1

DeepSpace
DeepSpace

Reputation: 81614

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

Upvotes: 3

Related Questions