简体   繁体   中英

How to get full spelling of month is using calender module in python pandas?

I have following table

NMonth    
01         
02
03

if i using following code(calender module)

SMonth=df['NMonth'].apply(lambda x: calendar.month_abbr[x])

My result will be :

NMonth    SMonth
01         Jan
02         Feb
03         Mar

How to get the full spelling of the month if using calender module? below is my desire result

NMonth    SMonth
01         January
02         February
03         March

Use month_name instead of month_abbr .

SMonth = df['NMonth'].apply(lambda x: calendar.month_name[x])

You could have found this answer yourself in the docs: https://docs.python.org/3.5/library/calendar.html#calendar.month_name

I think here is better use map by dictionary:

months = ['January','February','March','April','May','June','July','August',
          'September','October','November','December']

d = {'{:02d}'.format(i+1):months[i] for i in range(12)}
print (d)
{'05': 'May', '01': 'January', '06': 'June', '11': 'November', 
'04': 'April', '12': 'December', '09': 'September', '08': 'August', 
'03': 'March', '07': 'July', '10': 'October', '02': 'February'}

SMonth=df['NMonth'].map(d)
print (SMonth)

0     January
1    February
2       March
Name: NMonth, dtype: object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM