【问题标题】:Jupyter Dropdown ipywidget displays wrong dataframeJupyter Dropdown ipywidget 显示错误的数据框
【发布时间】:2021-05-20 17:19:57
【问题描述】:

我真的很困惑。第一次使用下拉小部件,如果这很明显,请原谅我,并感谢您提供的任何帮助。
这是我要显示的数据框及其构建方式:

def top_10_venues(data) :
    num_top_venues = 10
    indicators = ['st', 'nd', 'rd']

# create columns according to number of top venues
    columns = ['Neighborhood']
    for ind in np.arange(num_top_venues):
        try:
            columns.append('{}{} Most Common Venue'.format(ind+1, indicators[ind]))
        except:
            columns.append('{}th Most Common Venue'.format(ind+1))

# create a new dataframe
    neighborhoods_venues_sorted = pd.DataFrame(columns=columns)
    neighborhoods_venues_sorted['Neighborhood'] = data['Neighborhood']

    for ind in np.arange(denver_grouped.shape[0]):
        neighborhoods_venues_sorted.iloc[ind, 1:] = return_most_common_venues(data.iloc[ind, :], num_top_venues)

    neighborhoods_venues_sorted = neighborhoods_venues_sorted.set_index(['Neighborhood'])
top_10_venues(denver_grouped)
neighborhoods_venues_sorted

这是我的下拉小部件:

#Experimenting with Jupyter dropdown

filtered_df = None


dropdown = widgets.SelectMultiple(
                        options=neighborhoods_venues_sorted.index,
                        description='Venue',
                        disabled=False,
                        layout={'height':'100px', 'width':'40%'})

def max_density(widget):
    global filtered_df
    selection = list(widget['new'])

    with out:
        clear_output()
        display(neighborhoods_venues_sorted[selection])
        filtered_df = neighborhoods_venues_sorted[selection]

out = widgets.Output()
dropdown.observe(filter_dataframe, names='value')
display(dropdown)
display(out)

这是我最终看到的,我运行该函数的未格式化数据框?

【问题讨论】:

  • 与你得到的结果相比,我真的很难理解你想要的结果。在你的 max_density 函数中,添加一个print(selection) 语句并显示结果?请让您的代码复制粘贴可运行,您可以从字典等创建数据框吗?
  • 会做的,感谢您的意见。不明白为什么denver_neighnorhoods_sorted本身的输出是排好列的dataframe,而下拉出来的dataframe不一样?

标签: python drop-down-menu jupyter-notebook ipywidgets


【解决方案1】:

Booyah,想通了!
似乎我的问题是对创建邻域_场所_排序的单元格内发生的事情的误解。我以为我正在创建一个数据框。相反,我创建了一个函数

首先是排序功能

def return_most_common_venues(row, num_top_venues):
    row_categories = row.iloc[1:]
    row_categories_sorted = row_categories.sort_values(ascending=False)
    
    return row_categories_sorted.index.values[0:num_top_venues]

这是新功能,而不是单元格中的代码块
#Function to create sorted data frame with top 10 most common venues

def top_ten_venues(df) : 
    num_top_venues = 10
    indicators = ['st', 'nd', 'rd']
    
    # create columns according to number of top venues
    columns = ['Neighborhood']
    for ind in np.arange(num_top_venues):
        try:
            columns.append('{}{} Most Common Venue'.format(ind+1, indicators[ind]))
        except:
            columns.append('{}th Most Common Venue'.format(ind+1))
    
    neighborhoods_venues_sorted = pd.DataFrame(columns=columns)
    neighborhoods_venues_sorted['Neighborhood'] = df['Neighborhood'] 
    
    for ind in np.arange(denver_grouped.shape[0]):
        neighborhoods_venues_sorted.iloc[ind, 1:] = return_most_common_venues(df.iloc[ind, :], num_top_venues)
    #important to have a return in a function, this is the output that can be attached to a variable
    return neighborhoods_venues_sorted

接下来,我在目标数据帧上运行它并将其分配给一个变量。这解决了我的问题,我仍然太新,无法完全理解为什么当在单元格中运行完全相同的代码时,它拒绝将其分配为新的数据帧。
#creating a variable to hold the df for later access
neighborhoods_venues_sorted = top_ten_venues(denver_grouped)
#reindexing because it's fun
neighborhoods_venues_sorted = neighborhoods_venues_sorted.set_index(['Neighborhood'])

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    相关资源
    最近更新 更多