Reputation: 896
I have a dataframe called stock1
consisting of 505 stocks, and their percentage change for a total of 52 weekly rows for each column.
I wanted to find the stocks with the maximum value for the final weekly row, so I did:
import pandas as pd
st1 = pd.DataFrame({"FIN_MAX": stock1.tail(1).max().sort_values(ascending=False)})
The above code originally worked, but now it throws an error.
TypeError: '>' not supported between instances of 'float' and 'str'
The closest I can get to is if I drop the sort_values(ascending=False)
, but then the values are not arranged in order from highest to lowest.
st1 = pd.DataFrame({"FIN_MAX":stock1.tail(1).max()})
I've also tried the following code which also gives the same error as above.
st1.sort_values(["FIN_MAX"], ascending=False)
I'm using Python 3.6.1 on Jupyter Notebook. Any help or alternatives would greatly appreciated.
It seems the error persisted due to a column created called Unnamed: 0
when I concatenated all the stocks from individual csv files.
Once I did del stock1["Unnamed: 0"]
, it seems to have resolved the issue.
Upvotes: 1
Views: 362
Reputation: 294218
IIUC:
To get the stock with the largest value
stock1.iloc[-1].idxmax()
To get the stock with the largest value and the value
stock1.iloc[-1].nlargest(1)
To get the n
stocks with the largest values and their values
n = 5
stock1.iloc[-1].nlargest(n)
Upvotes: 1