4 C
London
Friday, December 20, 2024
HomePythonOperations in PythonHow to Create Pandas DataFrame from a String

How to Create Pandas DataFrame from a String

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories