Home > OS >  Cannot do inplace boolean setting on mixed-types with a non np.nan value
Cannot do inplace boolean setting on mixed-types with a non np.nan value

Time:06-11

I want to transform Data. This is my DataFrame (expense_overview)

Expense Proportion
0 Salaries 0.62
1 Advertising 0.15
2 Office Rent 0.15
3 Equipment 0.03
4 Utilities 0.03
5 Supples 0.01
6 Food 0.01

I want to replace the values of the Expense variable that has corresponding values in Proportion variable that are less then 5% with the value "Other".

mask = expense_overview.isin(expense_overview[expense_overview["Proportion"] < 0.05]["Expense"])

expense_overview[mask] = "Other"

But I get the following Error:

Cannot do inplace boolean setting on " TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value

CodePudding user response:

Pandas only solution:

expense_overview[lambda x: x.Proportion < 0.05] = expense_overview[lambda x: x.Proportion < 0.05].assign(Expense="Other")

Or using np.where:

expense_overview["Expense"] = np.where(expense_overview["Proportion"] < 0.05, "Other", expense_overview["Expense"])

CodePudding user response:

Are you trying to do this:

expense_overview.loc[expense_overview["Proportion"] < 0.05, "Expense"] = "Other"

Result:

       Expense  Proportion
0     Salaries        0.62
1  Advertising        0.15
2  Office Rent        0.15
3        Other        0.03
4        Other        0.03
5        Other        0.01
6        Other        0.01
  • Related