pandas - Python: unsupported operand type(s) for /: 'tuple' and 'float' -
i'm trying loop , create new data frame based on data frame have. suppose have dataframe 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 categorical variable such:
groups = pd.series(['foo','fizz','buzz'], dtype = "category") and want create new dataframe takes percentage of total , multiply scale. figured easiest way loop can have dataframe , names consistent it's throwing me error:
typeerror: unsupported operand type(s) /: 'tuple' , 'float' the code used below. appreciated (i know there has easier way). thanks!
df = pd.dataframe() #creating empty data frame j in categorical(groups).categories: #looping through categories calc = [] #empty list in range(0, demo.shape[0]): #loop through rows #below column divided total , multiplied scale. #then take number , append onto list calc.append(round((round(cross.ix[i,j],4)/round(cross.totals[i],4)) * cross.weight[i],4)) #finally append list dataframe using categories column name using setting enlargement df.loc[:,categorical(groups).categories[j]] = calc
round( (demo.ix[i,j],4) / round(demo.totals[i],4) ) i've added spaces code emphasize what's happening: have tuple of demo.ix[i,j] 1 element , 4 other, divide tuple demo.totals[i] rounded 4 places (a float), round that... can't round because attempting divide tuple float gives error saw. try following instead.
round(demo.ix[i,j],4) / round(demo.totals[i],4)
Comments
Post a Comment