Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

python - Plotly: How to subset a pandas dataframe at specific values in a column?

If I have a dataframe that looks like the following:

Time Wavelength Absorption
1 100 0.123
1 101 0.456
1 102 0.798
2 100 0.101
2 101 0.112
2 101 0.131
question from:https://stackoverflow.com/questions/66052159/plotly-how-to-subset-a-pandas-dataframe-at-specific-values-in-a-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You seem happy with the help you've already gotten in the comments, but since you've tagged your question with [plotly], I thought you might be be interested in how to set up a table that let's you select any subset of unique values of time; [1, 2] or the data as it is in your input dataframe.

Table 1 - Raw data

enter image description here

Table 2 - Subset with Time = 1

enter image description here

The comments in the code section should explain every step pretty clearly. Don't hesitate to let me know if anything should prove unclear.

Complete code:

import plotly.graph_objects as go
import pandas as pd

# input data
df = pd.DataFrame({'Time': {0: 1, 1: 1, 2: 1, 3: 2, 4: 2, 5: 2},
                     'Wavelength': {0: 100, 1: 101, 2: 102, 3: 100, 4: 101, 5: 101},
                     'Absorption': {0: 0.123,
                      1: 0.456,
                      2: 0.798,
                      3: 0.101,
                      4: 0.112,
                      5: 0.131}})

# plotly table setup
fig = go.Figure(go.Table(header=dict(values=df.columns.to_list()),
                         cells=dict(values=[df[col].to_list() for col in df.columns])
                        )
                     )

# set up buttons as list where the first element
# is an option for showing the raw data
buttons = [{'method': 'restyle',
                             'label': 'Raw data',
                             'args': [{'cells.values': [[df[col].to_list() for col in df.columns]],
                                       'header.values': [df.columns.to_list()]}, 
                                     ],
                              }]

# create a dropdown option for each unique value of df['Time']
# which in this case is [1, 2]
# and extend the buttons list accordingly
for i, option in enumerate(df['Time'].unique()):
    df_subset = (df[df['Time'] == option][['Wavelength', 'Absorption']])
    buttons.extend([{'method': 'restyle',
                             'label': 'Time = ' + str(option),
                             'args': [{'cells.values': [[df_subset[col].to_list() for col in df_subset.columns]],
                                       'header.values': [df_subset.columns.to_list()]}, 
                                     ],
                              },])

# configure updatemenu and add constructed buttons
updatemenus = [{'buttons': buttons,
                'direction': 'down',
                'showactive': True,}]

# update layout with buttons, and show the figure
fig.update_layout(updatemenus=updatemenus)
fig.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...