Reputation: 2300
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
Upvotes: 0
Views: 7570
Reputation: 49310
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)
Upvotes: 3