THIS is an updated version of the question providing a convenient feature
pd_read_printed(str_printed_df)
designed to create a pandas DataFrame from a string previously written using print (some_pandas_DataFrame):
def pd_read_printed(str_printed_df): global pd, StringIO try: x = pd except: import pandas as pd try: x = StringIO except: from pandas.compat import StringIO return pd.read_csv(StringIO(str_printed_df), delim_whitespace=True)
I compiled it for my own use after I have the answers to the following question:
I often see the contents of the pandas DataFrame in its print version on the Internet, for example, for example:
df1_as_string = """ Sp Mt Value count 4 MM2 S4 bg 10 5 MM2 S4 dgd 1 6 MM4 S2 rd 2 7 MM4 S2 cb 8 8 MM4 S2 uyi 8 """
The question arises: how to get a variable containing a DataFrame from a string variable in the style, for example:
df1 = pandas.someToMeUnknownPandasFunction(df1_as_string)
?
NOW you can use the supplied function to create DataFrame of df1_as_string :
df1 = pd_read_printed(df1_as_string)
and check if it works as expected:
print(df1)
gives:
Sp Mt Value count 4 MM2 S4 bg 10 5 MM2 S4 dgd 1 6 MM4 S2 rd 2 7 MM4 S2 cb 8 8 MM4 S2 uyi 8
source share