How can I create a Pandas DataFrame from a string in Python?

How can I create a Pandas DataFrame from a string in Python?

Creating a Pandas DataFrame from a string in Python involves using the pandas library to convert the string into a tabular data structure. This can be achieved by first importing the pandas library, then using the pandas.DataFrame() function to convert the string into a DataFrame. The string must be formatted in a specific way, with each row separated by a new line and each column separated by a comma. This process allows for efficient data manipulation and analysis, making it a useful tool for working with large datasets in Python.

Create Pandas DataFrame from a String


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.

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

Cite this article

stats writer (2024). How can I create a Pandas DataFrame from a string in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-pandas-dataframe-from-a-string-in-python/

stats writer. "How can I create a Pandas DataFrame from a string in Python?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-pandas-dataframe-from-a-string-in-python/.

stats writer. "How can I create a Pandas DataFrame from a string in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-pandas-dataframe-from-a-string-in-python/.

stats writer (2024) 'How can I create a Pandas DataFrame from a string in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-pandas-dataframe-from-a-string-in-python/.

[1] stats writer, "How can I create a Pandas DataFrame from a string in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a Pandas DataFrame from a string in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top