Mattr42
Mattr42

Reputation: 95

Python Pandas: AttributeError: 'DataFrame' object has no attribute 'str'

Using Python Pandas and importing an Excel document, the following code is kicking back an error:

df['City'] = df['City'].astype(str)
df['Rent'] = np.where((df['City'].str.contains('ST PETERSBURG')) & (df['BedroomsTotal'] == 2),  df['Rent'], df['LivingArea'] * df['Multiplier'])

The mentioned error is: AttributeError: 'DataFrame' object has no attribute 'str'

I added the top line to set the column to type string. They already should be strings since the column is a list of cities. Thoughts or suggestions on ways around this? Thanks.

enter image description here

Upvotes: 0

Views: 1211

Answers (1)

Yashar
Yashar

Reputation: 832

Try this:

df['Rent'] = df.apply(lambda x: x.Rent if ('ST PETERSBURG' in x.City) & (x.BedroomsTotal==2) else x.LivingArea*x.Multiplier, axis=1)

Check for the conditions.

Upvotes: 0

Related Questions