Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Construct a DataFrame in Pandas using string data in Python
Pandas DataFrames can be constructed from various data sources including CSV files. You can also create DataFrames directly from string data by using StringIO to simulate file-like input.
The StringIO wrapper from the io module allows us to treat string data as if it were being read from a file, making it compatible with pandas' CSV reading functions.
Example
Let us see how to construct a DataFrame using semicolon-separated string data ?
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)
The output of the above code is ?
Id Subject Course_Fee 0 10 DBMS 3000 1 11 Basic Maths 2000 2 12 Data Science 40000 3 13 Algorithm 5000
Using Different Separators
You can use different delimiters like comma, tab, or pipe characters ?
import pandas as pd
from io import StringIO
# Using comma separator
str_data = StringIO("""Name,Age,City
Alice,25,New York
Bob,30,London
Charlie,35,Paris""")
df = pd.read_csv(str_data, sep=",")
print(df)
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Paris
Key Points
-
StringIOcreates a file-like object from string data - Use the
sepparameter to specify the delimiter - The first line typically contains column headers
- This method is useful for testing or when data comes as formatted strings
Conclusion
Using StringIO with pd.read_csv() provides a convenient way to create DataFrames from string data. This approach is particularly useful for testing scenarios or when working with formatted text data that needs to be converted into a structured DataFrame format.
