• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Convert String to DataFrame in Python

Convert String to DataFrame in Python

Author: Aditya Raj
Last Updated: March 22, 2023

We use strings for text manipulation in Python. On the other hand, we use dataframes to handle tabular data in python. Despite this dissimilarity, we may need to convert a string to a pandas dataframe. This article discusses different ways to convert a string to a dataframe in python.

Table of Contents
  1. Convert String to DataFrame in Python
  2. Convert String to DataFrame Column
  3. JSON to Pandas DataFrame in Python
  4. Create DataFrame From Dictionary String in Python
  5. List String to DataFrame in Python
  6. Conclusion

Convert String to DataFrame in Python

To convert a string into a dataframe of characters in python, we will first convert the string into a list of characters using the list() function. The list() function takes the string as its input argument and returns a list of characters.

Next, we will pass this list to the DataFrame() function to create a dataframe using all the characters of the string. You can observe this in the following example.

import pandas as pd
myStr="PFB"
print("The string is:")
print(myStr)
myList=list(myStr)
df=pd.DataFrame(myList)
print("The output dataframe is:")
print(df)

Output:

The string is:
PFB
The output dataframe is:
   0
0  P
1  F
2  B

In the above example, we first converted the string "PFB" to a list of characters. Then, we used the DataFrame() function to create a dataframe from the list of characters.

Convert String to DataFrame Column

If you want to convert a string to a dataframe column, you can use the columns parameter in the DataFrame() function. When we pass a list of strings to the columns parameter in the DataFrame() function, the newly created dataframe contains all the strings as its column. 

To create a dataframe column from a string, we will first put the string into a list. Then, we will pass the list to the columns parameter in the DataFrame() function. After executing the DataFrame() function, we will get the dataframe with the given string as its column name as shown in the following example.

import pandas as pd
myStr="PFB"
print("The string is:")
print(myStr)
df=pd.DataFrame(columns=[myStr])
print("The output dataframe is:")
print(df)

Output:

The string is:
PFB
The output dataframe is:
Empty DataFrame
Columns: [PFB]
Index: []

In this example, you can observe that the string "PFB" is converted to a column of the output dataframe. This is due to the reason that we assigned the list containing the string to the columns parameter as an input argument.

JSON to Pandas DataFrame in Python

JSON strings are used to store and transmit data in software systems. Sometimes, we might need to convert a json string to a dataframe in python. For this, we will use the following step.

  • First, we will convert the json string to a python dictionary using the loads() method defined in the json module. The loads() method takes the json string as its input argument and returns the corresponding python dictionary.
  • Next, we will put the dictionary into a list. After that, we will pass the list to the DataFrame() function as input. 

After execution of the DataFrame() function, we will get the dataframe created from the json string. You can observe this in the following example.

import pandas as pd
import json
jsonStr='{"firstName": "John", "lastName": "Doe", "email": "john.doe@example.com", "age": 32}'
print("The json string is:")
print(jsonStr)
myDict=json.loads(jsonStr)
df=pd.DataFrame([myDict])
print("The output dataframe is:")
print(df)

Output:

The json string is:
{"firstName": "John", "lastName": "Doe", "email": "john.doe@example.com", "age": 32}
The output dataframe is:
  firstName lastName                 email  age
0      John      Doe  john.doe@example.com   32

Create DataFrame From Dictionary String in Python

To create a dataframe from a dictionary string, we will use the eval() function. The eval() function is used to evaluate expressions in python. When we pass a string containing a dictionary to the eval() function, it returns a python dictionary. 

After creating the dictionary, we will put it into a list and pass it to the DataFrame() function. After executing the DataFrame() function, we will get the output dataframe as shown below.

import pandas as pd
dictStr='{"firstName": "John", "lastName": "Doe", "email": "john.doe@example.com", "age": 32}'
print("The dictionary string is:")
print(dictStr)
myDict=eval(jsonStr)
df=pd.DataFrame([myDict])
print("The output dataframe is:")
print(df)

Output:

The dictionary string is:
{"firstName": "John", "lastName": "Doe", "email": "john.doe@example.com", "age": 32}
The output dataframe is:
  firstName lastName                 email  age
0      John      Doe  john.doe@example.com   32

In this example, we first converted the dictionary string into a dictionary. Then, we inserted the dictionary into a list. Finally, we converted the list of dictionaries to a dataframe using the DataFrame() function.

List String to DataFrame in Python

Instead of a dictionary string, you can also convert a list string to a dataframe using the eval() function and the DataFrame() function as shown in the following example.

import pandas as pd
listStr='[1,22,333,4444,55555]'
print("The list string is:")
print(listStr)
myList=eval(listStr)
df=pd.DataFrame([myList])
print("The output dataframe is:")
print(df)

Output:

The list string is:
[1,22,333,4444,55555]
The output dataframe is:
   0   1    2     3      4
0  1  22  333  4444  55555

Conclusion

In this article, we discussed different ways to convert a string to a dataframe in python. To learn more about python programming, you can read this article on how to convert a pandas series to a dataframe. You might also like this article on how to iterate rows in a pandas dataframe.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us