Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: invalid literal for int() with base 10: 'Sentiment Score'

I need to fix this, I’ve tried a lot of possibilities, but for some reason I don’t know, nothing works.

It should be fixed by adding int(float(x)), but can’t make it work.

I need to extract the symbol of this data, when the sentiment score is over 0.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data:

df    Negative  Neutral  Positive  Sentiment Score Symbol              Sector                       Industry    Market Cap                                Name
0   0.05721  0.87254   0.07026         0.028925    AIG  Financial Services          Insurance—Diversified  4.660713e+10  American International Group, Inc.
1   0.08450  0.83063   0.08484         0.007491    ALL  Financial Services  Insurance—Property & Casualty  3.632950e+10            The Allstate Corporation

and this is the code:

df_sellable = []
for i in range(0,len(df_strong_sell)-1):
    b= int(float(df_strong_sell.iloc[[i+1,'Sentiment Score']]*1000000))
    print("b",(b))
    if (b) >= 0:
        
        stringer=str(df_strong_sell.iloc[[i+1,'Symbol']])
        df_sellable.append(stringer)
        df_sellable.append(stringer)

I need to extract the symbol of this data, when the sentiment score is over 0.

But I get this error:

ValueError: invalid literal for int() with base 10: 'Sentiment Score'

>Solution :

Whenever you are tempted to use a for loop over a Dataframe, you should check yourself and verify that you are doing what pandas expects you to do.

Here is a way to get a list of symbols by filtering the Dataframe:

import pandas

df = pandas.DataFrame({
    "Neutral": [0.87254, 0.83063],
    "Sentiment Score": [0.028925, 0.007491],
    "Symbol": ["AIG", "ALL"],
})

# ------------------
# a list of bools indicating row by row if a row passes the test.
# ------------------
passes_test = df["Sentiment Score"] > 0.0
print(passes_test.tolist())
# ------------------

# ------------------
# rows passing the test
# ------------------
passing_test = df[passes_test]
print(passing_test)
# ------------------

# ------------------
# Get the particular column
# ------------------
print(passing_test["Symbol"].tolist())
# ------------------

# ------------------
# More conventionally done in one go
# ------------------
ids = df[df["Sentiment Score"] > 0.0]["Symbol"].tolist()
print(ids)
# ------------------

That will give you:

['AIG', 'ALL']
['AIG', 'ALL']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading