0

In summary, I'm trying to create a new column from an existing column, but I keep getting warning. I read data from listings for Airbnb, and I'm trying to create a new column based on 'transit' column containing the word 'subway'.

def extract(row, s):
    if type(row['transit']) == float:
        return 0
    if s in row['transit']:
        return 1
    else:
        return 0
df = pd.read_csv('listings.csv')
df1 = df[['transit', 'minimum_nights', 'price', 'reviews_per_month']]
df1['subway'] = df1.apply(extract, s='subway', axis=1)

I understand python doesn't particularly like the assignment on the last line, but I don't get the logic behind it in this case. I also tried using 'loc' and 'copy' but still got an error. What's the best way to address this warning:

/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:55: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
blueseal
  • 355
  • 3
  • 5
  • 15
  • 1
    Use `df1 = df[['transit', 'minimum_nights', 'price', 'reviews_per_month']].copy()` – jezrael May 06 '20 at 10:00
  • 1
    I saw the related question, but I didn't see the answer with .copy() as it was buried under more popular answer. That explains. – blueseal May 06 '20 at 10:06

0 Answers0