I have a list that was converted from a pandas DataFrame
:
[['2020.06.25 11:20:12', 'US500', 'sell', 1.0, 3047.3, '2020.06.25 11:21:32', 3051.4, 0.0, **-3.89**], ['2020.06.25 11:20:59', 'US500', 'sell', 1.0, 3049.8, '2020.06.25 11:21:33', 3051.6, 0.0, **-1.71**], ['2020.06.25 11:23:49', 'US500', 'sell', 1.0, 3051.6, '2020.06.25 11:25:32', 3049.7, 0.0, **1.8**]]
I wanted to calculate the percentage of times the number in bold is negative or positive for 'US500', that in the list can change with other strings like 'FB'.
So the output should look like:
US500: 60% positive, 40% negative FB: 70% positive, 30% negative etc.
I tried this:
ticker_list = df.values.tolist()
pos = [sum(y>=0 for y in x) for x in zip(ticker_list)]
but I got an error
TypeError: '>=' not supported between instances of 'list' and 'int'
and it wouldn't give what I want anyway.
Update:
With the new code it is possible to get positive and negative %, but it tries to save it, it doesn't iterate through the loop but just prints one value:
stocks = set([i[1] for i in ticker_list])
worksheet.write_column(3,0,stocks)
for s in stocks:
result = [i[-1] for i in ticker_list if s in i]
pos = (len([x for x in result if x > 0])/len(result))*100
neg = [100 - pos]
worksheet.write_column(3,1,pos)
worksheet.write_column(3,1,pos)
just save 1 value as the output:
I am not sure where FB is, I assume you will have it in your list. Also, I don't get why would you use
zip()
at all. Why would you make a list and not work on the DataFrame directly is also a mystery. Anyway, given your input (the initial list), the following code is enough.A more general ways would be: