AttributeError: 'str' object has no attribute 'news_headline'

Hi,



I am getting this error while running the following code from "Natural Language Processing in Trading". The file is taken from quantinsti course only - "news_headline_sentiments.csv".

I am running these lines of code:



for df in chunks:

    # Remove/drop missing values

    df.dropna(inplace=True)

    # Convert the words to lower string

    df.news_headline = df.news_headline.str.lower()



I am receiving the following error message:

 

AttributeError: 'str' object has no attribute 'news_headline'

But I see that  'news_headline' is in the CSV file, then how come I am receiving this error. 

Please help.

 

Hi Saurabh,



The error indicated your definition of "chunks" is incorrect.



At present it seems to be defined as a string, but it should be a pandas dataframe as defined in the code cell above as:

chunks = pd.read_csv('news_headline_sentiments.csv', chunksize=10000)



It would help if you could share your entire code.



Hope this helps!

 

Hi, 



I have a data frame in the name of "global_headlines_df"



Here's the code


Create empty lists

aapl_data =

amzn_data =

msft_data =



for df in global_headlines_df:

    # Select the headlines with the word 'apple' or 'aapl'

    data = df.loc[(df.headline.str.contains('aapl')) |

                  (df.headline.str.contains('apple'))]

    # Append the data into the empty aapl_data created

    aapl_data.append(data)



    # Select the headlines with the word 'amazon' or 'amzn'

    data = df.loc[(df.headline.str.contains('amzn')) |

                  (df.headline.str.contains('amazon'))]

    # Append the data into the empty amzn_data created

    amzn_data.append(data)



    # Select the headlines with the word 'microsoft' or 'msft'

    data = df.loc[(df.headline.str.contains('msft')) |

                  (df.headline.str.contains('microsoft'))]

    # Append the data into the empty msft_data created

    msft_data.append(data)



Error:

AttributeError: 'str' object has no attribute 'loc'

Please let me know what can be done.

Regards
Saurabh

Hi Saurabh,



The code you posted in the two posts seems different. Can you please share the notebook and data file on our email?



Thanks!

Hi Gaurav, 



I have already sent you an email.



Thanks & Regards

Saurabh Kamal

Hi Saurabh,



Have shared the response on email. There was a slight error, the chunk data was not being used in the loop.



Hope this helps!