0
import pandas as pd
import datetime
import numpy as np

df = pd.read_excel('/content/drive/My Drive/python/NSE 010120-181020.xlsx')

df.head()

df['Date'] = pd.to_datetime(df['Date'] , format='%Y-%m-%d')

df['Date'] = pd.to_datetime(df['Date'] , format='%d-%m-%Y')

df.info()

Date = df['Date']

Date

from datetime import datetime

df['Percentage Change'] = ((df['Open'] - df['Close'])/100).astype(float)

df['Percentage Change']



df['Marker'] = np.nan

for i in range(len(df['Marker'])):
  if df['Percentage Change'][i] > 0:
    df['Marker'][i] =='Profit'
  elif df['Percentage Change'][i] < 0:
    df['Marker'][i] = 'Loss'
  elif df['Percentage Change'][i] == 0:
    df['Marker'][i] = 'Flat'

df['Marker']

AttributeError                            Traceback (most recent call last)
<ipython-input-82-e9145deac6fc> in <module>()
  1 for i in range(len(df['Marker'])):
  2   if df['Percentage Change'][i] > 0:
  ----> 3     (df['Marker'][i] =='Profit').copy(deep=True)
  4   elif df['Percentage Change'][i] < 0:
  5     (df['Marker'][i] == 'Loss').copy(deep=True)

  AttributeError: 'bool' object has no attribute 'copy'

I am getting the marker values as NaN but the loss values are printing as desired.Why are the profit & Flat values are nan but not the Loss value ? And what is the meaning of this error AttributeError: 'bool' object has no attribute 'copy'

0       NaN
1      Loss
2       NaN
3       NaN
4       NaN
   ... 
195     NaN
196     NaN
197    Loss
198     NaN
199    Loss
  • Please also provide [reproducible sample data](https://stackoverflow.com/questions/20109391). Otherwise potential answerers cannot test. This question looks simple enough, but for another time you may not have much luck. – Bill Huang Oct 18 '20 at 17:18

1 Answers1

0

You mistyped = with ==. Replace df['Marker'][i] =='Profit' by df['Marker'][i] = 'Profit' and see if it works.

N.B. If the dataframe doesn't update itself, also replace df['Marker'][i] = by df.at[i, 'Marker'] =. This is the standard syntax for assigning a value to a single cell in-place.

Bill Huang
  • 4,491
  • 2
  • 13
  • 31