0

I got it to work once, but did not save the file and lost the code. Please help.

 import pandas as pd
 import nltk

 df0.head(4)

#   X   Y   month   day FFMC    DMC DC
# 0 7   5   mar fri 86.2    26.2    94.3
# 1 7   4   oct tue 90.6    35.4    669.1


 monthdict={'jan':1,'feb':2'mar':3,'oct':10,'nov':11,'dec':12}

 def month2num(month):
    return monthdict[month]
 df0['month'] = df0['month'].apply(month2num)
 df0.head()

'Comment' I am not that smart, and just getting started, so kindly would someone please explain the solution in English.

Error printout below:

# KeyError                                  
# Traceback 
# (most recent call last)
# <ipython-input-48-566f3675aaed> in <module>()
#       def month2num(month):
#           return monthdict[month]
# ----> df0['month'] = df['month'].apply(month2num)
#       df0.head()

# 1 frames
# pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

# <ipython-input-48-566f3675aaed> in month2num(month)
#        
#        def month2num(month):
# ---->      return monthdict[month]
#        df0['month'] = df['month'].apply(month2num)
#        df0.head()

# KeyError: 'apr'
2tan2ten
  • 109
  • 1
  • 9
  • 1
    the error says that your dict `monthdict` does not have `apr` key. – Rakesh Jun 10 '19 at 07:17
  • you can just do `pd.to_datetime(df.month,format='%b').dt.month` instead of the function – anky Jun 10 '19 at 07:22
  • you dont have april in your month dictionary, your code is fine (though there are better solutions obviously). – E.Serra Jun 10 '19 at 08:31
  • Thank you for your observation E.Serra, I only used some of the months for this, as an example. – 2tan2ten Jun 10 '19 at 08:41
  • 1
    anky_91, Your solution worked great. It printed all the columns and rows and changed the month name to its corresponding number. – 2tan2ten Jun 10 '19 at 08:43
  • @2tan2ten sure no problem, closing this question(actually a dupe :) ) – anky Jun 10 '19 at 08:52

3 Answers3

2

An alternative to a dictionary could be using the index in a tuple (although hashing using a dictionary might be faster):

months = (None, 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec')
integervalue = months.index(monthstring)
Pete
  • 421
  • 3
  • 6
1

Use datetime:

df['month'] = pd.to_datetime(df['month'],format='%b').dt.month

Or:

import calendar
df['month'] = df['month'].str.title().apply(list(calendar.month_abbr).index)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

You can convert the month to integer with below given code

import pandas as pd

def GetMonthInInt(month):
    MonthInInts = pd.Series([1,2,3,4,5,6,7,8,9,10,11,12],index=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'])
    return MonthInInts[month.lower()]

data = pd.DataFrame(['Oct','Nov','Mar','Feb','Jan','Dec','Aug','Sep'],columns=['Month'])
data['MonthInInt']= data['Month'].apply(GetMonthInInt)
print(data)

You will get this output with above sample code

  Month  MonthInInt
0   Oct          10
1   Nov          11
2   Mar           3
3   Feb           2
4   Jan           1
5   Dec          12
6   Aug           8
7   Sep           9

Hope, this may solve your problem to convert month to integer.

Himmat
  • 166
  • 5