Saving a dataframe

Is there anyway to save a dataframe to my local pc? I’m trying to use:

if st.button(‘save dataframe’):
df.to_csv('mydf.csv)

But nothing happens when I click the button.

1 Like

Hi @Okita,

As far as I can tell you’re just missing the “writing to disk” part of the script that would accomplish what you want. I just tried this script and it worked – it wrote “df.csv” into the directory where streamlit was run when I pressed the button.

import streamlit as st
import pandas as pd

df = pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']})

st.write(df)
if st.button('save dataframe'):
    open('df.csv', 'w').write(df.to_csv())

This is assuming that you are simply trying to write the CSV to disk.

Does this accomplish what you need?

1 Like

Ohhh, ok! That’s it, thanks!

2 Likes

Oh great! Apologies for the long delay in response. We’re working on it!

1 Like