3

I'm attempting to create a new column for each column by dividing two columns. df is a pandas dataframe...

columns = list(df.columns.values)
for column_1 in columns: 
    for column_2 in columns:         
        new_column = '-'.join([column_1,column_2])
        df[new_column] = df[column_1] / df[column_2]

Getting an error: NotImplementedError: operator '/' not implemented for bool dtypes

Any thoughts would be appreciate?

3
  • One of the columns is a Boolean value. Check the df.dytpes, and omit the columns that aren't numeric. Commented Sep 12, 2015 at 1:18
  • Thank you... I assumed bool would evaluate to 0 or 1. Clearly it does not. Commented Sep 12, 2015 at 1:42
  • I would try to convert both columns to float, and catch any errors from the attempted conversion. If Python can cast to float, it's probably fair game to go ahead with the division. This also prevents any integer division weirdness. Commented Sep 12, 2015 at 15:35

1 Answer 1

3

Like Brian said you're definitely trying to divide non-numeric columns. Here's a working example of dividing two columns to create a third:

name = ['bob','sam','joe']
age = [25,32,50]
wage = [50000, 75000, 32000]

people = {}
for i in range(0,3):
    people[i] = {'name':name[i], 'age':age[i],'wage':wage[i]}

# you should now have a data frame where each row is a person
# you have one string column (name), and two numerics (age and wage)        
df = pd.DataFrame(people).transpose()

df['wage_per_year'] = df['wage']/df['age']

print df
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.