第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從 Plotly 散點(diǎn)圖上選擇的數(shù)據(jù)點(diǎn)創(chuàng)建 Pandas DataFrame

從 Plotly 散點(diǎn)圖上選擇的數(shù)據(jù)點(diǎn)創(chuàng)建 Pandas DataFrame

幕布斯7119047 2022-06-07 19:01:00
Plotly Figure Widget 幫助我創(chuàng)建一個(gè)交互式散點(diǎn)圖,即,我可以在散點(diǎn)圖上選擇數(shù)據(jù)點(diǎn),并根據(jù)選擇我的表小部件顯示記錄。我需要幫助將此表轉(zhuǎn)換為熊貓數(shù)據(jù)框。import plotly.graph_objs as goimport plotly.offline as pyimport pandas as pdimport numpy as npfrom ipywidgets import interactive, HBox, VBoxpy.init_notebook_mode()df = pd.read_csv('https://raw.githubusercontent.com/jonmmease/plotly_ipywidget_notebooks/master/notebooks/data/cars/cars.csv')f = go.FigureWidget([go.Scatter(y = df['City mpg'], x = df['City mpg'], mode = 'markers')])scatter = f.data[0]N = len(df)scatter.x = scatter.x + np.random.rand(N)/10 *(df['City mpg'].max() - df['City mpg'].min())scatter.y = scatter.y + np.random.rand(N)/10 *(df['City mpg'].max() - df['City mpg'].min())scatter.marker.opacity = 0.5def update_axes(xaxis, yaxis):    scatter = f.data[0]    scatter.x = df[xaxis]    scatter.y = df[yaxis]    with f.batch_update():        f.layout.xaxis.title = xaxis        f.layout.yaxis.title = yaxis        scatter.x = scatter.x + np.random.rand(N)/10 *(df[xaxis].max() - df[xaxis].min())        scatter.y = scatter.y + np.random.rand(N)/10 *(df[yaxis].max() - df[yaxis].min())axis_dropdowns = interactive(update_axes, yaxis = df.select_dtypes('int64').columns, xaxis = df.select_dtypes('int64').columns)# Create a table FigureWidget that updates on selection from points in the scatter plot of ft = go.FigureWidget([go.Table(    header=dict(values=['ID','Classification','Driveline','Hybrid'],                fill = dict(color='#C2D4FF'),                align = ['left'] * 5),    cells=dict(values=[df[col] for col in ['ID','Classification','Driveline','Hybrid']],               fill = dict(color='#F5F8FF'),               align = ['left'] * 5))])def selection_fn(trace,points,selector):    t.data[0].cells.values = [df.loc[points.point_inds][col] for col in ['ID','Classification','Driveline','Hybrid']]scatter.on_selection(selection_fn)# Put everything togetherVBox((HBox(axis_dropdowns.children),f,t))只是期望在將散點(diǎn)圖上的點(diǎn)選擇到熊貓數(shù)據(jù)框后創(chuàng)建的表。
查看完整描述

3 回答

?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

可能不是解決它的最優(yōu)雅的方法,但在你選擇你的點(diǎn)之后,你可以輸入:

d = t.to_dict()
df = pd.DataFrame(d['data'][0]['cells']['values'], index =d['data'][0]['header']['values']).T

t 是類型plotly.graph_objs._figurewidget.FigureWidget

我使用 jupyter notebook,所以我在代碼下方的一個(gè)單元格中編寫了這些代碼行,我得到了一個(gè)包含所選事件的新 df


查看完整回答
反對(duì) 回復(fù) 2022-06-07
?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

假設(shè)以下代碼突出顯示您關(guān)心的點(diǎn):

def selection_fn(trace,points,selector):
    t.data[0].cells.values = [df.loc[points.point_inds][col] for col in ['ID','Classification','Driveline','Hybrid']]

更改它以返回?cái)?shù)據(jù)框:

def selection_fn(trace,points,selector):
    return pd.df([df.loc[points.point_inds][col] for col in ['ID','Classification','Driveline','Hybrid'] if col in {selection}])

列表推導(dǎo)需要更改為僅循環(huán)您要返回的點(diǎn)。文檔中的示例列表理解:

[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]


查看完整回答
反對(duì) 回復(fù) 2022-06-07
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

更好的解決方案:


    def selection_fn(trace, points, selector):

    

        t.data[0].cells.values = [

            df.loc[points.point_inds][col]

            for col in ["ID", "Classification", "Driveline", "Hybrid"]]

        

        selection_fn.df1 = df.loc[points.point_inds]



    print(selection_fn.df1)


查看完整回答
反對(duì) 回復(fù) 2022-06-07
  • 3 回答
  • 0 關(guān)注
  • 543 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)