pythonpandasseabornline-plot

How to plot pct_change for many stock names


I have a data set with prices of 6 major stocks i.e., Google, Amazon etc.

My plan is to create a plot which would show a percent change, pct_change()of column known as close_value.

As you can see my ticker_symbol is an object. I tried and changed it to float because of the string error but then I lost all ticker names i.e. I executed returns.close_value.plot();.

How not to lose stock names while plotting?

Data display

Pct_change for data set ]

Data info


Solution

  • Does this work?

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    # Create Sample DataFrame
    df1 = pd.DataFrame({'day_date': ['2020-05-28', '2020-05-27', '2020-05-26', '2020-05-22'], 
                        'ticker_symbol': ['AAPL', 'AAPL','TSLA','TSLA'],
                        'close_value': [318, 400, 500, 450]})
    
    # Convert to Timestamp format
    df1['day_date'] = pd.to_datetime(df1['day_date'])
    
    # Store % Change in new Column
    df1['pct_change_close_value'] = df1['close_value'].pct_change()
    
    # Fill null value with 0
    df1['pct_change_close_value'].fillna(0, inplace = True)
    
    # Display
    display(df1)
    
    # Check Data types of columns
    display(df1.dtypes)
    
    # Use Seaborn to plot
    sns.lineplot(data = df1, x = 'day_date', y = 'pct_change_close_value', hue = 'ticker_symbol')
    

    You just need to set hue = ticker_symbol in sns plot.

    enter image description here