Если вы хотите преобразовать строку типа Dec \'18 (HOZ18)
в December 18
, вот одно из решений.
1) Определите функцию для очистки строк:
# define a dictionary to convert short month names to full ones
month_mapper = {
'Jan': 'January',
'Feb': 'February',
'Mar': 'March',
'Apr': 'April',
'May': 'May',
'Jun': 'June',
'Jul': 'July',
'Aug': 'August',
'Sep': 'September',
'Oct': 'October',
'Nov': 'November',
'Dec': 'December',
}
def clean_month_string(s):
# replace the '\' char with empty string
s = s.replace('\\', '')
# split into three pieces on space
# eg, "Dec '18 (HOZ18)" ->
# month = "Dec"
# year = "'18"
# code = "(HOZ18)"
month, year, code = s.split(' ')
# convert month using month mapper
month = month_mapper[month]
# remove the ' at the start of the year
year = year.replace("'", "")
# return new month and new year (dropping code)
return ' '.join([month, year])
2) Примените эту функцию к каждой строке в вашем DataFrame, используя apply
.
# drop that first row, which is not properly formatted
df = df.drop(0).reset_index(drop=True)
# apply the function to your 'Contracts' series.
df['Contracts'] = df['Contracts'].apply(clean_month_string)