Pandas Dataframe from a String
To load data from a string into a Pandas Dataframe, you can leverage the io.StringIO class. This approach is particularly useful for testing purposes.
Consider the following test data:
TESTDATA="""col1;col2;col3 1;4.4;99 2;4.5;200 3;4.7;65 4;3.2;140 """
To create a Dataframe from this string:
if sys.version_info[0] < 3: from StringIO import StringIO else: from io import StringIO
TESTDATA = StringIO("""col1;col2;col3 1;4.4;99 2;4.5;200 3;4.7;65 4;3.2;140 """)
import pandas as pd df = pd.read_csv(TESTDATA, sep=";")
This code will create a Dataframe named 'df' containing the columns 'col1', 'col2', and 'col3' with the values defined in the string.
The above is the detailed content of How to Create a Pandas DataFrame from a String?. For more information, please follow other related articles on the PHP Chinese website!