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.