Home » How to Create Pandas DataFrame from a String

How to Create Pandas DataFrame from a String

by Erma Khan

You can use the following basic syntax to create a pandas DataFrame from a string:

import pandas as pd
import io   

df = pd.read_csv(io.StringIO(string_data), sep=",")

This particular syntax creates a pandas DataFrame using the values contained in the string called string_data.

The following examples show how to use this syntax in practice.

Example 1: Create DataFrame from String with Comma Separators

The following code shows how to create a pandas DataFrame from a string in which the values in the string are separated by commas:

import pandas as pd
import io

#define string
string_data="""points, assists, rebounds
5, 15, 22
7, 12, 9
4, 3, 18
2, 5, 10
3, 11, 5
"""

#create pandas DataFrame from string
df = pd.read_csv(io.StringIO(string_data), sep=",")

#view DataFrame
print(df)

   points   assists   rebounds
0       5        15         22
1       7        12          9
2       4         3         18
3       2         5         10
4       3        11          5

The result is a pandas DataFrame with five rows and three columns.

Example 2: Create DataFrame from String with Semicolon Separators

The following code shows how to create a pandas DataFrame from a string in which the values in the string are separated by semicolons:

import pandas as pd
import io

#define string
string_data="""points;assists;rebounds
5;15;22
7;12;9
4;3;18
2;5;10
3;11;5
"""

#create pandas DataFrame from string
df = pd.read_csv(io.StringIO(string_data), sep=";")

#view DataFrame
print(df)

   points   assists   rebounds
0       5        15         22
1       7        12          9
2       4         3         18
3       2         5         10
4       3        11          5

The result is a pandas DataFrame with five rows and three columns.

If you have a string with a different separator, simply use the sep argument within the read_csv() function to specify the separator.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Convert Pandas DataFrame Columns to Strings
How to Convert Timestamp to Datetime in Pandas
How to Convert Datetime to Date in Pandas

Related Posts