df=pd.DataFrame({'dept':['dept1','dept2','dept3','dept4','dept5'],
'room1':['0','1','1','NA','1'],
'room2':['1','0','NA','1','1'],
'room3':['0','0','1','NA','1'],
'room4':['1','NA','1','1','1'],
'count':['4','3','3','2','4']}
dept room1 room2 room3 room4 count
0 dept1 0 1 0 1 4
1 dept2 1 0 0 NA 3
2 dept3 1 NA 1 1 3
3 dept4 NA 1 NA 1 2
4 dept5 1 1 1 1 4
i have a selectbox where the user can filter the required data and display records based on his selection.
expected result:
if user select dept2 :
dept room1 room2 room3
0 dept2 1 0 0
if user select dept4:
dept room2 room4
0 dept4 1 1
code:
option_dept = df["dept"].unique().tolist()
selected_dept = st.multiselect("search by departement",option_dept)
if selected_dept:
df= df[df["dept"].isin(selected_dept)]
st.write(df)
the problem is that with this code all the columns are displayed
how can i remove the columns that includes NA or null in each selected row?
CodePudding user response:
Select only dept
and room
columns, replace possible strings NA
to NaN
s and remove missing columns:
df= df[df["dept"].isin(selected_dept)].filter(regex='room|dept').replace('NA', np.nan).dropna(axis=1)
Or:
df= df[df["dept"].isin(selected_dept)].drop('count', axis=1).replace('NA', np.nan).dropna(axis=1)
CodePudding user response:
Suppose the user selects dept2
then this code would give you the desired output,
pd.DataFrame(df.loc[1, :].dropna()).T
Output -
dept | room1 | room2 | room3 | count | |
---|---|---|---|---|---|
1 | dept2 | 1 | 0 | 0 | 3 |
For other dept
values, just change the row number in the iloc
function. You can even set the indices of the dataframe to the dept
value for that row using df.set_index("dept")
and then use df.loc["dept_2", :]
to get the data for that row.