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}})
vestland
  • 55,229
  • 37
  • 187
  • 305
gopher
  • 35
  • 5
  • 1
    `df[df['Time'] == 1]][['Wavelength', 'Absorption']]` – Epsi95 Feb 04 '21 at 19:03
  • `df.query('Time=1')` or `df[df['Time']==1]`? – Quang Hoang Feb 04 '21 at 19:03
  • ```res = (df[df['Time']==1]).drop('Time',axis=1)``` – sophocles Feb 04 '21 at 19:03
  • Epsi95 answer worked. Thank you. `df2 = df1[df1['Time'] == 1][['Wavelength', 'Absorption']]` – gopher Feb 04 '21 at 20:19
  • @gopher Next time, please include a sample dataset as described [here](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254). 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! – vestland Feb 05 '21 at 08:40
  • @gopher How did my suggestion work out for you? – vestland Feb 05 '21 at 16:37
  • Hi @vestland Thanks for your help. Yeah, I'm **_really_** new to any sort of coding, and so I'm still at a stage where it's not so easy or natural to look at a dataframe in a code format (as opposed to the table format). However, the way you wrote the 'Absorption' column does make things more comprehensible. I'll probably start doing that. Thanks! And yeah, I'll try to be a bit better in the future with my posts. I literally just had no idea what I was doing. – gopher Feb 06 '21 at 07:21

2 Answers2

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()
vestland
  • 55,229
  • 37
  • 187
  • 305
1

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

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

gopher
  • 35
  • 5