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

Advertisements

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.

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']

Leave a ReplyCancel reply