0
votes

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

1
May you share your sample dataset ?A DUBEY
Yep. Please see screenshotMattr42
What are you expecting the .str to do?Jiří Baum
Ultimately to solve the following problem: stackoverflow.com/q/65313183/11144854Mattr42

1 Answers

0
votes

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.