Cummulative 3D animation scatter plot

Hi All,

I am trying to make a 3D figure where I have multiple points in the X,Y, and Z axis. I have no issue creating the 3D scatter plot but am running into an issue where I would like to animate the data cumulatively over a J axis. There are multiple X,Y, and Z values per J value. Ive followed another post with a similar issue but I believe the problem arises with the multiple values per J axis.

Below is a rough example of the data I am working with:

X Y X J
1 2 3 1
2 2 2 1
2 3 4 1
1 2 3 2
2 2 2 2
2 5 7 2

Here is the post I have worked with in trying to build my figure.

Below is my code so far:

def threeDscatter(data):
  #Import Data
  x, y, z, j = data['x'], data['y'], data['z], data['j']

  #Create Figure
  fig = go.Figure(data =[go.Scatter3d( x = x, y = y, z = z,
                                      mode='markers',
                                      marker=dict(size=3,
                                                  color = j, 
                                                  colorscale='Viridis'))])
  
  fig.update_layout(
      scene = dict(
          xaxis=dict(range=[min(x), max(x)], autorange=False),
          yaxis=dict(range=[min(y), max(y)], autorange=False),
          zaxis=dict(range=[min(z), max(z)], autorange=False),))

  frames = [go.Frame(data= [go.Scatter3d(
                                       x=x[:k+1], 
                                       y=y[:k+1],
                                       z=z[:k+1])],
                   
                   traces= [0],
                   name=f'frame{k}'      
                  )for k  in  range(len(x)-1)]
  fig.update(frames=frames)
  
  fig.update_layout(title = 'X V. Y V. Z', 
                    width=1000, 
                    height=1000, 
                    scene = dict(xaxis_title='x',
                                 yaxis_title='y',
                                 zaxis_title='z'))
  fig.update_layout(updatemenus=[dict(type="buttons",
                          buttons=[dict(label="Play",
                                        method="animate",
                                        args=[None, dict(frame=dict(redraw=True,fromcurrent=True, mode='immediate'))])])])
  fig.show()