I an a newbie with both Python and Pandas. I have a pandas dataframe consisting of many years of timeseries data of a number of stocks e.g. S&P 500. I want to iterate through each unique stock code one by one and calculate technical indicators on the closing prices. I have been trying to create a new dataframe for each unique stock from the main dataframe with all its price history and then pass it to a method which will do the technical analysis rather than passing the main dataframe. Here is some sample data from my dataframe:
Id Symbol Date Open High Low Close Volume
1 A99 2012-01-02 730.019 730.019 730.019 730.019 0
2 ABA 2012-01-02 4.200 4.200 4.200 4.200 0
3 AFI 2012-01-02 5.360 5.360 5.360 5.360 0
4 AIA 2012-01-02 2.520 2.520 2.520 2.520 0
...
501 A99 2012-01-03 730.019 730.019 730.019 730.019 0
...
I have tried indexers such as loc, iloc and also groupby but with no luck. I have read many articles e.g. Select rows from a DataFrame based on values in a column in pandas but none of them quite match my requirements. The main issue with all of these is you have to have a literal search criteria whereas I want a variable filter name i.e. the stock name. Sample of my datatable is as follows:
This is a snippet of my current code which does not work:
# Get the data from the database df = stockPrices.get_data()
# Create technical indicators for each distinct stock
# First get a series of all unique stock codes
ts = pd.Series(df.Symbol.unique())
# Iterate through the series and call the technical indicator method
for row in ts:
# filter for just this stock
filtered_df = df.loc[df['Symbol'] == row]
df = stockPrices.calc_technicals(filtered_df, row)
Any pointers would be appreciated.