in Education by
I have three columns like follows: Sales Population Income Price 14 48777.0 285.0 nan 17 12550.0 1.0 nan 24 15664.0 14.0 14 9 23796.0 24.0 0 20 40149.0 63.0 nan 75 39489.0 32.0 nan I need to convert everything to per capita terms, that is for each element in sales, I need to divide it by the corresponding population. For example, for sales, I want to divide it by population to get per capita sales: Per capita sales 14/48777 17/12550 ... The command I use is: data['Per Capita Sales'] = data['Sales'] / data['Population'] But I got the following error: TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' Does anyone know how to fix this? I have searched online how to divide one column by another column element-wise, but did not find a safe way to do it... Since the columns with values "nan" and "0" are in the numerator, I don't know why there could be an error... Thank you very much for your help! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Most probably, at least one of the columns involved is of string type. Instead of converting them only for the division, I propose to convert them to numbers in advance, for all other operations: data.Sales = pd.to_numeric(data.Sales) data.Population = pd.to_numeric(data.Population) As I see, all rows contain valid data, so the above (shortened) form should be enough. But if your data were "dirty" (some entries not convertible to number), you should add errors='coerce' parameter. Then your original command to divide should work. If you are not sure about types of columns in your DataFrame, execute: data.info() Or maybe you load your DataFrame from a file, e.g. calling read_csv? In this case include dtype parameter (a dictionary in the form {"column name" : type}) to force the required types of particular columns.

Related questions

0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have a dataset |category| cat a cat b cat a I'd like to be able to return something like (showing unique values ... cat a 2 cat b 1 Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I want to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I don ... 'gdp', 'cap'] Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How to delete a column in a DataFrame, currently I am using: del df['column_1'] this is working fine, ... expected df.column_name ? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have a list of files in a folder in my system file_list= ["A", "B", "C"] I Have read the files using ... C How do I accomplish this? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have data like this: id a b c d 1 y y z z 2 y z y y 3 y y y y I want to count the value of "y" ... 1 2 2 1 3 4 Can anyone help me? Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have col1 in a pandas df. I want to make col2: col1 col2 1 1 1 2 1 3 1 4 2 2 ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I want an efficient way to append one string to another in Python. var1 = "foo" var2 = "bar" var3 = var1 + ... built-in method to use? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Im performing some operations in a df of 4000 columns and 17520 rows. I have to repeat these operations ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
...