SHARE
TWEET

Untitled

a guest Oct 15th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Deal with Pandas boolean
  2. df["Active"] = np.where(df["Active"] == "Y", True, False)
  3.  
  4. # Converter
  5. def convert_percent(val):
  6.     """
  7.     Convert the percentage string to an actual floating point percent
  8.     - Remove %
  9.     - Divide by 100 to make decimal
  10.     """
  11.     new_val = val.replace('%', '')
  12.     return float(new_val) / 100
  13.  
  14. # Convert dtypes when reading data
  15. df = pd.read_csv("sales_data_types.csv",
  16.                    dtype={'Customer Number': 'int'},
  17.                    converters={'2016': convert_currency,
  18.                                '2017': convert_currency,
  19.                                'Percent Growth': convert_percent,
  20.                                'Jan Units': lambda x: pd.to_numeric(x, errors='coerce'),
  21.                                'Active': lambda x: np.where(x == "Y", True, False)
  22.                               })
  23.  
  24. df.dtypes
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top