Problems with Python & Pandas: Adding calculated column to dataframe that includes data from a function provides error

I am trying to add a calculated column: "NetEarnings" to my DataFram "Wages". The "NetEarnings" column subtracting Tax from AnnualIncome; tax is drawing from a function I created to calculate taxes. It will not let me add this new column due to an error:

"TypeError: unsupported operand type(s) for -: 'float' and 'tuple'"

I have tried mostly everything, unsure if I made a dumb mistake somewhere. Thanks for your help!

Code:

def Tax(AnnualIncome):
    if (0 < AnnualIncome) & (AnnualIncome <= 21450):
        return (.15 * AnnualIncome)
    elif (21450 < AnnualIncome) & (AnnualIncome <= 51900):
        return (3215.5 + ((AnnualIncome - 21450) * .28))
    else: 
        return (11,743.5 + ((AnnualIncome - 51900) * .31))

wages['Tax'] = wages['AnnualIncome'].apply(Tax)

# Problem line
wages['NetEarning'] = wages['AnnualIncome'] - wages['Tax']

Error:

TypeError: unsupported operand type(s) for -: 'float' and 'tuple'

Additionally to Ente answer,

I suggest to use np.where instead of apply. Apply is faster than a for loop, but much slower than apply.

A possible solution would be:

np.where(df['AnnualIncome'] <= 21450, (.15 *df['AnnualIncome']), 
               np.where(df['AnnualIncome'] <= 51900,
                        (3215.5 + ((df['income']- 21450) * .28)),
                              (11743.5 + (df['AnnualIncome'] - 51900) * .31)
                ) 
        )

All the things I hate about Python | by Nathan Murthy, Python's problems are obvious to just about every engineer who writes serious mission-critical, production-grade code in the modern enterprise� These exercises are nothing but Python assignments for the practice where you need to solve different questions and problems. The solution is provided for each practice question. Using these exercises, you can practice various Python problems, questions, programs, and challenges. All exercises are tested on Python 3. Each Exercise has 10 Questions.

The following line returns a tuple because of the ,:

        return (11,743.5 + ((AnnualIncome - 51900) * .31))

Try:

        return (11743.5 + ((AnnualIncome - 51900) * .31))

Disadvantages of Python, Runtime Errors: One of the major drawbacks of this language is that its design has numerous issues. Python programmers face several issues regarding the design of the language. This language requires more testing and also it has errors that only show up at runtime this is because the language is dynamically typed. Practice Python coding with fun, bite-sized challenges. Earn XP, unlock achievements and level up. It's like Duolingo for learning to code.

As much as you can, avoid DataFrame.apply which is a hidden loop. Since your function uses logical, mutually exclusive conditions, consider numpy.select to process assignment as whole arrays and not row wise scalar values:

condlist = [(0 < wages['AnnualIncome']) & (wages['AnnualIncome'] <= 21450),
            (21450 < wages['AnnualIncome']) & (wages['AnnualIncome'] <= 51900),
            (0 >= wages['AnnualIncome']) | (wages['AnnualIncome'] > 51900)]

choicelist = [(.15 * wages['AnnualIncome']),
              (3215.5 + ((wages['AnnualIncome'] - 21450) * .28)),
              (11,743.5 + ((wages['AnnualIncome'] - 51900) * .31))]

wages['Tax'] = np.select(condlist, choicelist)
wages['NetEarning'] = wages['AnnualIncome'] - wages['Tax']

What are the drawbacks of Python?, Python seems all the rage these days, and not undeservingly - for it is truly a language with which one almost enjoys being given a new problem to solve. But, as� Today the Windows team announced the May 2019 Update for Windows 10.In this post we’re going to look at what we, Microsoft’s Python team, have done to make Python easier to install on Windows by helping the community publish to the Microsoft Store and, in collaboration with Windows, adding a default “python.exe” command to help find it.

What are the problems with Python as a Programming Language , This however isn't the problems unless speed is focal point for the project. In other words unless high speed is a requirement, benefits offered by Python are� Python is a high-level, interpreted and general-purpose dynamic programming language that focuses on code readability. The syntax in Python helps the programmers to do coding in fewer steps as…

Why Python?, You might be wondering "Why should I solve problems with Python?" There are other programming languages in the world such as MATLAB, LabView, C++ and � Welcome to Practice Python! There are over 30 beginner Python exercises just waiting to be solved. Each exercise comes with a small discussion of a topic and a link to a solution. New exercise are posted monthly, so check back often, or follow on Feedly, Twitter, or your favorite RSS reader.

The 10 Most Common Mistakes That Python Developers Make, The problem here is that the except statement does not take a list of exceptions specified in this manner. Rather, In Python 2.x, the syntax�

Comments
  • the number 11.743.5 in my original answer is invalid as well. Is that supposed to be 11.7435 or 11743.5? From the context I'd assume 11743.5.