Search code examples
pythonpython-3.xdataframeloopsplotly

How can we loop through rows and columns of a dataframe and plot each column?


I have a dataframe that looks like this.

date    symbol  2023-03-01  2023-03-02  2023-03-03  2023-03-04  2023-03-05  2023-03-06  2023-03-07  2023-03-08  2023-03-09  2023-03-10  2023-03-11  2023-03-12
0   AAPL    3   4   1   4   0   0   0   1   2   3   1   1
1   AMGN    0   2   2   4   0   4   3   1   2   0   0   2
2   AXP     2   0   0   0   1   1   1   3   1   1   0   2
3   BA  2   2   0   4   2   4   4   0   0   4   0   2
4   CAT     2   3   4   3   1   1   1   3   1   1   4   0

I'm trying to use plotly to do a scatter plot of columns of data. I tested the code sample below.

import plotly.express as px
for i, column in enumerate(df.columns,i):
    print(column)
    for index, row in df.iterrows():
      #print(index)
      print(row[column])
      fig = px.scatter(df, x=row[column], y=row[column], color=row[column], hover_data='symbol')
      fig.show()

I get this error.

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['symbol', datetime.date(2023, 3, 1), datetime.date(2023, 3, 2), datetime.date(2023, 3, 3), datetime.date(2023, 3, 4), datetime.date(2023, 3, 5), datetime.date(2023, 3, 6), datetime.date(2023, 3, 7), datetime.date(2023, 3, 8), datetime.date(2023, 3, 9), datetime.date(2023, 3, 10), datetime.date(2023, 3, 11), datetime.date(2023, 3, 12)] but received: AAPL

Somehow, my approach is wrong, but I'm not sure what the issue is.


Solution

  • The problem in your code is that you are passing the value of the column as the "x" and "y" arguments in "px.scatter()", and this should be the names of the columns, not the values themselves.

    To fix the issue, you need to modify the "px.scatter()" function call to use the column names as the "x" and "y" arguments.

    Also, you can simplify your loop by using the "df.iterrows()" method directly. Here's an updated version of your code: import plotly.express as px

    for index, row in df.iterrows():
        fig = px.scatter(df, x='date', 
        y=row['symbol'], color=row['symbol'], 
        hover_data='symbol')
        fig.show()
    

    As you see, 'date' is used as the "x" argument because it seems to be the common column for all rows. You may replace 'date' with the appropriate column name if it's different in your actual dataframe.