Construct a DataFrame in Pandas using string data in Python

PythonServer Side ProgrammingProgramming

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.

Example

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
   10;DBMS;3000
   11;Basic Maths;2000
   12;Data Science;40000
   13;Algorithm;5000
   """)
df = pd.read_csv(str_data, sep =";")
print(df)

Output

Id Subject Course_Fee
0 10 DBMS 3000
1 11 Basic Maths 2000
2 12 Data Science 40000
3 13 Algorithm 5000
raja
Published on 27-Aug-2020 17:41:35
Advertisements