Construct a DataFrame in Pandas using string data in Python

Here we will see how we can construct a pandas dataframe using string type data. Pandas supports csv files, but we can do the same using string also. For string type data, we have to use one wrapper, that helps to simulate as the data is taken as csv reader.

Here we are using a string that takes data and separated by semicolon.


Let us see the following implementation to get better understanding −

import pandas as pd
from io import StringIO
str_data = StringIO("""Id;Subject;Course_Fee
   11;Basic Maths;2000
   12;Data Science;40000
df = pd.read_csv(str_data, sep =";")


Id Subject Course_Fee
0 10 DBMS 3000
1 11 Basic Maths 2000
2 12 Data Science 40000
3 13 Algorithm 5000

Updated on: 27-Aug-2020


Kickstart Your Career

Get certified by completing the course

Get Started