# How to compute the percentage of values based on multiple columns

• Python
• msn009
In summary, the code is trying to calculate the percentage of occurrences based on the values in 2 columns, but is giving a zero value. The solution is to change the operator to and to add a float value in the beginning.

#### msn009

I have a dataframe as shown in the picture and what I am trying to do is to calculate the number of occurrences based on the values in 2 columns and then calculate the percentage of the occurrences. I have tried the following code but it gives me a zero value in the end and i don't know why.

Code:
count_a2_x = (df['a1'].str.contains('b') & df['a2'].str.contains('x')).value_counts()[True]
count_a2_y = (df['a1'].str.contains('b') & df['a2'].str.contains('y')).value_counts()[True]
acc  = float(count_a2_x/ (count_a2_x + count_a2_y))

the expected output should be 3/6 = 0.5

#### Attachments

• p2.png
726 bytes · Views: 383
msn009 said:
I have a dataframe as shown in the picture and what I am trying to do is to calculate the number of occurrences based on the values in 2 columns and then calculate the percentage of the occurrences. I have tried the following code but it gives me a zero value in the end and i don't know why.

Code:
count_a2_x = (df['a1'].str.contains('b') & df['a2'].str.contains('x')).value_counts()[True]
count_a2_y = (df['a1'].str.contains('b') & df['a2'].str.contains('y')).value_counts()[True]
acc  = float(count_a2_x/ (count_a2_x + count_a2_y))

the expected output should be 3/6 = 0.5
Use and instead of &. The & operator is the bitwise and operator.

when i changed it to and it gave me this error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

and I tried now to add float and it seems to work when float is assigned in the beginning.

Code:
count_a2_x = float((df['a1'].str.contains('b') & df['a2'].str.contains('x')).value_counts()[True])
count_a2_y = float((df['a1'].str.contains('b') & df['a2'].str.contains('y')).value_counts()[True])
acc  = count_a2_x/ (count_a2_x + count_a2_y)

## What is the formula for computing the percentage of values based on multiple columns?

The formula for computing the percentage of values based on multiple columns is: Percentage = (Value of a specific column / Total of all columns) * 100.

## How do I compute the percentage of values based on multiple columns in a spreadsheet?

To compute the percentage of values based on multiple columns in a spreadsheet, you can use the "SUM" function to get the total of all columns and then divide the value of the specific column by the total and multiply by 100.

## Can I compute the percentage of values based on multiple columns in a database query?

Yes, you can use the "SUM" function in your database query to get the total of all columns and then use the same formula as in a spreadsheet to compute the percentage of values based on multiple columns.

## Do I need to convert the values to decimals before computing the percentage?

No, you do not need to convert the values to decimals. As long as you use the correct formula, the percentage will be accurate regardless of the number format.

## What is the purpose of computing the percentage of values based on multiple columns?

The purpose of computing the percentage of values based on multiple columns is to understand the distribution and proportion of each column in relation to the total. This can help in making data-driven decisions and identifying trends or patterns.