Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

Copying dataframe based off column names in a list (for using dcc.checklist and line plots)

Hi all,

I’m trying to copy data from a dataframe df to a second dataframe dff based off the names of the columns which are held in a list. This is for a dashboard which will use a checklist to display selected data on a line graph, and is based off the tutorial by Adam of charming data. The relevant code here would go in the update_graph() function in his example.

To copy specific columns of a dataframe I’d usually write:

df = pd.read_csv('supercoolcsvfile.csv')

options_chosen=['Column A',
                'Column B',
]

dff=df[['Column A', 'Column B']]

I tried this, but it doesn’t work:

df = pd.read_csv('supercoolcsvfile.csv')

options_chosen=['Column A',
                'Column B',
]

dff=df[[options_chosen]]

Returning the error:

KeyError: "None of [Index([('Column A, 'Column B)')], dtype='object')] are in the [columns]"

hi @oanyc
:wave: welcome to the Dash community.

I’m glad you found the answer quickly and that you shared it here. For this question to appear as “solved”, can you please take out the code after the Edit section and post it as a second post/reply to this thread. Then choose “solution”.
That way, community members will see this question as solved.

Thank you,

1 Like

Wound up finding a solution 19 minutes after posting this :sweat_smile:

df = pd.read_csv('supercoolcsvfile.csv')

options_chosen=['Column A',
                'Column B',
]

dff=df.get(options_chosen)
1 Like