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.
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. Theloads()
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": "[email protected]", "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": "[email protected]", "age": 32}
The output dataframe is:
firstName lastName email age
0 John Doe [email protected] 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": "[email protected]", "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": "[email protected]", "age": 32}
The output dataframe is:
firstName lastName email age
0 John Doe [email protected] 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!
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.