Easily convert percentage values stored as strings (e.g., “85%”) into numeric values for analysis in Python! ๐
Why Use This Function? ๐ค
- ๐ Data Cleaning: Many datasets store percentages as strings, which are not suitable for calculations.
- ๐งน Preprocessing: Converting these to numbers is essential for machine learning and data analysis.
Function Example ๐
def str_to_rate(s):
if pd.isnull(s) == False:
return float(s.replace('%', ''))
else:
return s
How to Use ๐ ๏ธ
col = "ReplyRate"
df[col] = df[col].apply(str_to_rate)
- ๐ This will convert all percentage strings in the column to float numbers (e.g., “85%” โ 85.0).
- โ ๏ธ Null values will remain unchanged.