@ray.hilll
To find the return of stock tickers in rows of a dataframe, you can use the following steps:
Here is an example code snippet to illustrate the above steps:
1 2 3 4 5 6 7 8 |
import pandas as pd
# Assume your dataframe is named 'df' with columns 'Ticker' and 'Close'
df['Return'] = df.groupby('Ticker')['Close'].pct_change()
average_return = df.groupby('Ticker')['Return'].mean()
# Print the average returns for each stock
print(average_return)
|
This will calculate the daily returns for each stock and then calculate and print the average return for each stock ticker.
Remember to customize the code according to your dataframe structure and column names.
@ray.hilll
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import pandas as pd
# Create a sample dataframe
data = {'Ticker': ['AAPL', 'AAPL', 'AAPL', 'GOOG', 'GOOG', 'GOOG'],
'Close': [100, 105, 110, 500, 510, 520]}
df = pd.DataFrame(data)
# Calculate daily returns for each stock
df['Return'] = df.groupby('Ticker')['Close'].pct_change()
# Calculate average return for each stock
average_return = df.groupby('Ticker')['Return'].mean()
# Print the average returns for each stock
print(average_return)
|
In this example, we first create a sample dataframe df with two columns 'Ticker' and 'Close'. We then calculate the daily returns for each stock using the pct_change function. Next, we calculate the average return for each stock by grouping the dataframe by 'Ticker' and applying the mean function to the 'Return' column. Finally, we print the average returns for each stock ticker.