-1

I have a string that is in the csv format, and I am wondering whether I can save it as a data frame without first saving it as a csv and then importing it as one.

Thanks!

2
  • 3
    please read this before asking a question. Commented May 26, 2022 at 22:38
  • @Riceblades btw, there is shorthand built into the side for that link: [ask] results in How to Ask. Commented May 26, 2022 at 23:24

1 Answer 1

0

Yes, one of the possible approaches is to convert the string into a StringIO object and pass it to pd.DataFrame

from io import StringIO
import pandas as pd

string_data = StringIO("""col1,col2
    1,"a"
    2,"b"
    3,"c"
    4,"d"
    """)

df = pd.read_csv(string_data)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.