I have a column which contains number of months in string and int format. Need to convert it into just integers. (eg 12)
df1=pd.DataFrame({'Term':["12"," ","12 Months","12months","12mthsb","12 *4months"]})
Output:
df1=pd.DataFrame({'Term':["12","12","12","12","12"]})
Tried using str.replace(r'\D', '')
and str.replace(r'[^0-9]', '')
but then the row with just the number (ie 12) gets replaced by NaN
You probably have int/float/str mix in the column
You can try to convert to str and then replace:
df1['someColumn'].astype(str).str.replace(r'\D', '')
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.