how can I make this work? (plotly-python)

Viewed 44

I've reached a point where I don't know what else to do.

I have this df

dff8df      !!!0 WWSF   VPIP   VPIP   VPIP
0         52.0  38.64  38.64  38.64
1         62.0  50.00  50.00  50.00
2         73.0  56.25  56.25  56.25
3         99.0  23.08  23.08  23.08
4         30.0  41.67  41.67  41.67
..         ...    ...    ...    ...
540       50.0  18.75  18.75  18.75
541       99.0  26.32  26.32  26.32
542       50.0  28.57  28.57  28.57
543       83.0  14.29  14.29  14.29
544       57.0  38.89  38.89  38.89

[545 rows x 4 columns]

and this code:

print("dff8df",dff8)

    figure = px.scatter(data_frame=dff8,
                   x=dff8[:,drop1],
                   y=dff8[:,drop2],
                   color=dff8[:,drop4],
                   size=dff8[:,drop3],
                   trendline="ols",
                   trendline_color_override="red",
                   title="%s vs %s"%(drop1, drop2),
                   )

and it gives this error with plotly in python:

pandas.errors.InvalidIndexError: (slice(None, None, None), 'VPIP')

How can I solve this problem?

1 Answers

A number of areas to consider

  • this is a pandas error not a plotly error
  • you have not defined drop1 etc so I can only assume you mean them to be column names. Hence have defined as first four columns of your sample data
  • your sample data looks problematic, multiple columns with name VPIP the data frame created from read_csv()
dff8df !!!0 WWSF VPIP VPIP.1 VPIP.2
0 52 38.64 38.64 38.64 nan
1 62 50 50 50 nan
2 73 56.25 56.25 56.25 nan
3 99 23.08 23.08 23.08 nan
  • dff8[:,drop1] is invalid pandas code to select a column. This should be dff8.loc[:, drop1]. Additionally why so slice in this way when you can just pass column names to plotly express

working code

import io
import pandas as pd
import plotly.express as px

dff8 = pd.read_csv(
    io.StringIO(
        """dff8df      !!!0 WWSF   VPIP   VPIP   VPIP
0         52.0  38.64  38.64  38.64
1         62.0  50.00  50.00  50.00
2         73.0  56.25  56.25  56.25
3         99.0  23.08  23.08  23.08
4         30.0  41.67  41.67  41.67
540       50.0  18.75  18.75  18.75
541       99.0  26.32  26.32  26.32
542       50.0  28.57  28.57  28.57
543       83.0  14.29  14.29  14.29
544       57.0  38.89  38.89  38.89"""
    ),
    sep="\s+",
    index_col=0,
)

drop1, drop2, drop3, drop4 = dff8.columns[0:4].tolist()

figure = px.scatter(
    data_frame=dff8,
    x=dff8.loc[:, drop1],
    y=dff8.loc[:, drop2],
    color=dff8.loc[:, drop4],
    size=dff8.loc[:, drop3],
    trendline="ols",
    trendline_color_override="red",
    title="%s vs %s" % (drop1, drop2),
)


figure
Related