Python: unsupported operand type(s) for /: 'tuple' and 'float'

Minh Mai

I'm trying to loop and create a new data frame based on another data frame I have. Suppose I have a dataframe like this

Foo Fizz Buzz totals scale
10  3    2     15     .2
8   4    3     15     .2 
5   1    5     11     .4
6   7    5     18     .1
9   2    6     17     .1

And a categorical variable as such:

groups = pd.Series(['Foo','Fizz','Buzz'], dtype = "category")

And I want to create a new dataframe where it takes the percentage of the total and multiply it by the scale. I figured the easiest way is to loop it so I can have the dataframe and names consistent but it's throwing me this error:

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

The code I used is below. Any help would be appreciated (I know there has to be an easier way). Thanks!

df = pd.DataFrame() #creating an empty data frame 
for j in Categorical(groups).categories: #looping through categories
    calc = [] #empty list
    for i in range(0, demo.shape[0]): #loop through rows
        #Below is basically the column divided by the total and multiplied by the scale. 
        #Then take that number and append it onto the list                    
        calc.append(round((round(cross.ix[i,j],4)/round(cross.totals[i],4)) * cross.weight[i],4))

        #finally append this list to the dataframe using the categories as the column name using setting with enlargement 
        df.loc[:,Categorical(groups).categories[j]] = calc 
TigerhawkT3
round(   (demo.ix[i,j],4) / round(demo.totals[i],4)   )

I've added spaces to your code to emphasize what's happening: you have a tuple of demo.ix[i,j] for one element and 4 for the other, then you divide that tuple by demo.totals[i] rounded to 4 places (a float), then you round that... only you can't round that because attempting to divide that tuple by a float gives the error you saw. Try the following instead.

round(demo.ix[i,j],4) / round(demo.totals[i],4)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

unsupported operand types for //: 'str', 'int'

From Dev

python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

From Dev

Python TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

From Dev

unsupported operand type(s) for +: 'int' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'

From Dev

unsupported operand types for * : 'float' and 'decimal'

From Dev

TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

From Dev

Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

From Dev

Unsupported operand type(s) for +: 'float' and 'str' error

From Dev

Unsupported operand types 'NoneType'

From Dev

Django unsupported operand types

From Dev

python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

From Dev

Python Type Error Unsupported Operand

From Dev

Unsupported operand type in Python

From Dev

Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

From Dev

TypeError: unsupported operand type in Python

From Dev

TypeError: unsupported operand type(s) for &: 'float' and 'float', but I have no &

From Dev

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

From Dev

TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

From Dev

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

From Dev

Python 3: unsupported operand type(s) for /: 'float' and 'bytes'

From Dev

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

From Dev

TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType' in Python 3

From Dev

TypeError: unsupported operand type(s) for <<: 'int' and 'float'

From Dev

TypeError: unsupported operand type(s) for +: 'Timestamp' and 'float'

From Dev

TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

From Dev

Unsupported operand types for bitwise exclusive OR in Python

Related Related

  1. 1

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

  2. 2

    unsupported operand types for //: 'str', 'int'

  3. 3

    python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

  4. 4

    Python TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

  5. 5

    unsupported operand type(s) for +: 'int' and 'tuple'

  6. 6

    TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'

  7. 7

    unsupported operand types for * : 'float' and 'decimal'

  8. 8

    TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

  9. 9

    Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

  10. 10

    Unsupported operand type(s) for +: 'float' and 'str' error

  11. 11

    Unsupported operand types 'NoneType'

  12. 12

    Django unsupported operand types

  13. 13

    python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

  14. 14

    TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

  15. 15

    Python Type Error Unsupported Operand

  16. 16

    Unsupported operand type in Python

  17. 17

    Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

  18. 18

    TypeError: unsupported operand type in Python

  19. 19

    TypeError: unsupported operand type(s) for &: 'float' and 'float', but I have no &

  20. 20

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

  21. 21

    TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

  22. 22

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

  23. 23

    Python 3: unsupported operand type(s) for /: 'float' and 'bytes'

  24. 24

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

  25. 25

    TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType' in Python 3

  26. 26

    TypeError: unsupported operand type(s) for <<: 'int' and 'float'

  27. 27

    TypeError: unsupported operand type(s) for +: 'Timestamp' and 'float'

  28. 28

    TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

  29. 29

    Unsupported operand types for bitwise exclusive OR in Python

HotTag

Archive