【问题标题】:Plotly: How to subset a pandas dataframe at specific values in a column?Plotly:如何在列中的特定值处对熊猫数据框进行子集化?
【发布时间】:2021-02-04 19:01:11
【问题描述】:

如果我有一个如下所示的数据框:

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

我想创建一个新的数据框,它只包含 Time = 1 时的行,并且只有 Wavelength 和 Absorption 列:

Wavelength Absorption
100 0.123
101 0.456
102 0.798

我该怎么做呢?

样本数据:

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}})

【问题讨论】:

  • df[df['Time'] == 1]][['Wavelength', 'Absorption']]
  • df.query('Time=1') 或 df[df['Time']==1]?
  • res = (df[df['Time']==1]).drop('Time',axis=1)
  • Epsi95 答案有效。谢谢你。 df2 = df1[df1['Time'] == 1][['Wavelength', 'Absorption']]
  • @gopher 下一次,请包含here 中描述的示例数据集。由于这是您的第一篇文章,我已经为您完成了。请记住,您将节省自己和那些试图回答您未来问题的人大量时间,只需花费三分钟时间来学习提供的链接中的方法.正如您提供的那样,您的表格肯定是一个好的开始,但是在代码 sn-p 中准备好 df 会更好。编码愉快!

标签: python pandas plotly


【解决方案1】:

您似乎对在 cmets 中获得的帮助感到满意,但由于您已使用 [plotly] 标记您的问题,我想您可能对如何设置一个让您选择任何时间唯一值的子集; [1, 2] 或输入数据框中的数据。

表 1 - 原始数据

表 2 - Time = 1 的子集

代码部分的 cmets 应该非常清楚地解释每个步骤。如果有任何不清楚的地方,请随时告诉我。

完整代码:

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()

【讨论】:

  • 哦!这真的很方便。谢谢
【解决方案2】:

@Epsi95 的这个答案对我有用。在这里发布它作为答案而不是评论:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多