How to adjust dataframe column names in streamlit

Question:

I am trying to display only selected columns from the pandas dataframe. logic is working but columns come with extra characters added to them.

Selected columns come inside Parentheses, quotes and coma. I got stuck on finding from where does it come and how to get rid of it.

Please Help.

import pandas as pd
import streamlit as st


df = pd.read_csv('data/AUDCAD_1h.csv')

st.sidebar.header('Data Filter')
col = st.sidebar.multiselect("Select any column",
                             df.columns)
if not col:
    data = df.copy()
else:
    data = df[col].values
    data = pd.DataFrame(data, columns=[col])
st.write(data)
Asked By: dbmtbm

||

Answers:

instead of

if not col:
    data = df.copy()
else:
    data = df[col].values
    data = pd.DataFrame(data, columns=[col])
st.write(data)

N.B. Streamlit multiselect option returns a list.

Just use:

if not col:
    data = df.copy()
else:
    data = data[col]
st.write(data)
Answered By: EMT
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.