gpt4 book ai didi

Python - 动态选择新数据框的列

转载 作者:行者123 更新时间:2023-12-05 03:22:27 27 4
gpt4 key购买 nike

我正在寻找一种解决方案来从数据框中动态选择两列(例如使用 ipywidgets 或 steamlit)并使用该选择创建一个新的数据框。

目的是允许用户从更大的数据集中选择两列,以允许过滤这两列以删除 NaN 以进行回归和绘图。我不想输入每个列标题,因为每次使用时数据框都会更改。任何帮助都会有很大帮助!

import ipywidgets as widgets
import pandas as pd

df = pd.DataFrame({'A' : [4,NaN], 'B' : [10,20], 'C' : [100,50], 'D' : [-30,-50]})

x_choice = widgets.Dropdown(
options=list(df.select_dtypes('number').columns)[0:],
description='Number:',
disabled=False,
)

y_choice = widgets.Dropdown(
options=list(df.select_dtypes('number').columns)[1:],
description='Number:',
disabled=False,
)


dfX = pd.DataFrame(x_choice, y_choice)
dfX.dropna()

最佳答案

这个使用的是 streamlit 方法。使用 st.dataframe() 显示时,框架的样式看起来更好,但您可以忽略它。

代码

"""
Creates a new datafame based on selected column from existing dataframe.
"""

import pandas as pd
import streamlit as st


df = pd.DataFrame({'A' : [4,None], 'B' : [10,20],
'C' : [100,50], 'D' : [-30,-50],
'E' : [1500,800], 'F' : [0.258,1.366]})

# Apply styler so that the A column will be displayed with integer value because there is None in it.
df_style = df.style.format(precision=2, na_rep='MISSING', thousands=",", formatter={('A'): "{:.0f}"})

st.write('Current dataframe')
st.dataframe(df_style)

# We use a form to wait for the user to finish selecting columns.
# The user would press the submit button when done.
# This is done to optimize the streamlit application performance.
# As we know streamlit will re-run the code from top to bottom
# whenever the user changes the column selections.
with st.form('form'):
sel_column = st.multiselect('Select column', df.columns,
help='Select a column to form a new dataframe. Press submit when done.')
drop_na = st.checkbox('Drop rows with missing value', value=True)
submitted = st.form_submit_button("Submit")

if submitted:
dfnew = df[sel_column]
if drop_na:
dfnew = dfnew.dropna()

st.write('New dataframe')
dfnew_style = dfnew.style.format(precision=2, na_rep='MISSING', thousands=",", formatter={('A'): "{:.0f}"})
st.dataframe(dfnew_style)

输出

enter image description here

关于Python - 动态选择新数据框的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72719231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com