0

Here is the code I used:

school_spending_df["Per Student Spending"] = pd.cut(school_spending_df["Per Student Budget"], 
                                              bins, labels=spending_labels, 
                                              include_lowest=True)
school_spending_df

This is the error:

TypeError                                 Traceback (most recent call last)
/var/folders/kd/9g_zryd90yz1qzjl3rghyc040000gq/T/ipykernel_4152/634257724.py in <module>
      1 # Use `pd.cut` to categorize spending based on the bins.
      2 
----> 3 school_spending_df["Per Student Spending"] = pd.cut(school_spending_df["Per Student Budget"], 
      4                                               bins, labels=spending_labels,
      5                                               include_lowest=True)

~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/reshape/tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest, duplicates, ordered)
    290             raise ValueError("bins must increase monotonically.")
    291 
--> 292     fac, bins = _bins_to_cuts(
    293         x,
    294         bins,

~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/reshape/tile.py in _bins_to_cuts(x, bins, right, labels, precision, include_lowest, dtype, duplicates, ordered)
    425 
    426     side: Literal["left", "right"] = "left" if right else "right"
--> 427     ids = ensure_platform_int(bins.searchsorted(x, side=side))
    428 
    429     if include_lowest:

TypeError: '<' not supported between instances of 'int' and 'str'

The 'Per Student Budget' column is an object, having trouble converting it to integer because of the special characters. Like "$".

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • `p.cut` should only apply to numeric data and you should ask how convert your data to numerics. make minimal and reproducible example of your dataset https://stackoverflow.com/help/minimal-reproducible-example – Panda Kim Apr 27 '23 at 02:42

0 Answers0