Is there a way to extract the specific integer value and add it to a list from python, so in this case 88, 151, 212 and 276?

av_link = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=' + 
ticker + '&outputsize=full&apikey=test&datatype=csv'

df_stock = pd.read_csv(av_link)

print(df_stock)
index_earning_value = []
print(actual_date_name)
for y in actual_date_name:
    index_earning_value.append(df_stock.index[df_stock['timestamp']==str(y)])

print(index_earning_value)

This is the ouput of the list:

[Int64Index([88], dtype='int64'), Int64Index([151], dtype='int64'), Int64Index([212], dtype='int64'), Int64Index([276], dtype='int64'), Int64Index([], dtype='int64')]

>Solution :

Use extend instead append:

index_earning_value.extend(df_stock.index[df_stock['timestamp']==str(y)].tolist())

Alternative is use list comprehension with flattening:

index_earning_value = [y for y in actual_date_name 
                         for y in df_stock.index[df_stock['timestamp']==str(y)]]

Leave a Reply