Why am I getting TypeError: '<' not supported between instances of 'int' and 'str' with pandas' cut() function?
Question:
I’m receiving TypeError: '<' not supported between instances of 'int' and 'str'
while using pandas’ cut() function. More specifically, the usage is
spending_bins = [0, 585, 630, 645, 680]
labels = ["<$585", "$585-630", "$630-645", "$645-680"]
school_spending_df = per_school_summary.copy()
school_spending_df["Spending Ranges (Per Student)"] = pd.cut(school_spending_df["Per Student Budget"], bins=spending_bins, labels=labels)
school_spending_df.head()
looks like this.
But exactly which strings and integers are being compared and clashing here?
I can’t think of what to try, other than leaving off the labels since it’s an optional argument, which has not effect, nor did setting the arguments right
and include_lowest
.
Answers:
Try converting dtype of Per Student Budget
column into float:
school_spending_df["Spending Ranges (Per Student)"] = pd.cut(school_spending_df["Per Student Budget"].str[1:].astype(float), bins=spending_bins, labels=labels)
I’m receiving TypeError: '<' not supported between instances of 'int' and 'str'
while using pandas’ cut() function. More specifically, the usage is
spending_bins = [0, 585, 630, 645, 680]
labels = ["<$585", "$585-630", "$630-645", "$645-680"]
school_spending_df = per_school_summary.copy()
school_spending_df["Spending Ranges (Per Student)"] = pd.cut(school_spending_df["Per Student Budget"], bins=spending_bins, labels=labels)
school_spending_df.head()
looks like this.
But exactly which strings and integers are being compared and clashing here?
I can’t think of what to try, other than leaving off the labels since it’s an optional argument, which has not effect, nor did setting the arguments right
and include_lowest
.
Try converting dtype of Per Student Budget
column into float:
school_spending_df["Spending Ranges (Per Student)"] = pd.cut(school_spending_df["Per Student Budget"].str[1:].astype(float), bins=spending_bins, labels=labels)