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

Hi!
I just start working with streamlit but I think itā€™s awesomeā€¦ butā€¦ I canā€™t find any more advanced funtion in documentatnion :frowning:

And now I have a problem with save dataframe as file. I do everything like above:

if len(data_frame) == 0:
    st.write("Proszę załadować plik z osobnikami by uzyskać wynik i wygenerować raport.")
else:
    min = st.number_input('Od:', 0, len(data_frame), 0)
    max = st.number_input('Do:', 0, len(data_frame), len(data_frame) )
    dataset = st.container()
    table = st.dataframe(data_frame.iloc[min:max])

    if st.button('GENERUJ RAPORT DLA ZAKRESU'):
        open('table', 'w').write(table.to_csv())

but I got:

StreamlitAPIException : to_csv() is not a valid Streamlit command.

So itā€™s not working anymore in my example. How can I write that table as file?

Best regards! :slight_smile:

P.S.
Sorry for refreshing old thread but I think itā€™s a right place cos searching in internet solution, I got that link :slight_smile: