Table of Contents
To create a Pandas DataFrame from a string, you must first parse the string into a list of lists. Each item in the list will represent a row in the DataFrame. Then, you must use the DataFrame constructor to create the DataFrame, passing in the list of lists as the argument. Finally, you can modify the columns and rows of the DataFrame as needed.
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.