1

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

I want to create a new dataframe that only contains the rows when Time = 1, and only has the Wavelength and Absorption columns:

Wavelength Absorption
100 0.123
101 0.456
102 0.798

How would I go about doing this?

Sample data:

import pandas as pd
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}})
7
  • 1
    df[df['Time'] == 1]][['Wavelength', 'Absorption']] Commented Feb 4, 2021 at 19:03
  • df.query('Time=1') or df[df['Time']==1]? Commented Feb 4, 2021 at 19:03
  • res = (df[df['Time']==1]).drop('Time',axis=1) Commented Feb 4, 2021 at 19:03
  • Epsi95 answer worked. Thank you. df2 = df1[df1['Time'] == 1][['Wavelength', 'Absorption']] Commented Feb 4, 2021 at 20:19
  • @gopher Next time, please include a sample dataset as described here. Since this is your first post I've done it for you. Just remember that you'll save yourself and those who attempt to answer your future questions a lot of time by investing the three minutes it takes to learn the approach in the provided link. Your tables, as you have provided them, is certainly a good start, but having a df ready in a code snippet is better. Happy coding! Commented Feb 5, 2021 at 8:40

2 Answers 2

2

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 lets 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()
Sign up to request clarification or add additional context in comments.

1 Comment

Oh! This is really convenient. Thanks
1

This answer from @Epsi95 worked for me. Posting it here as an answer rather than a comment:

df[df['Time'] == 1]][['Wavelength', 'Absorption']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.