Constructing a DataFrame in Pandas using string data means creating a pandas DataFrame where the values are strings instead of numbers. For example: making a table of student names and their cities using text values like "Alice", "Bob", "Delhi", "London".
Using a dictionary of strings
Pandas easily converts this dictionary into a DataFrame. Each key represents a column name and values are stored as lists of strings.
Example: This example shows how to build a DataFrame directly from a dictionary of string lists.
import pandas as pd
data = {
"Date": ["10/2/2011", "11/2/2011", "12/2/2011", "13/2/2011"],
"Event": ["Music", "Poetry", "Theatre", "Comedy"],
"Cost": ["10000", "12000", "5000", "8000"]
}
df = pd.DataFrame(data)
print(df)
Output
Date Event Cost 0 10/2/2011 Music 10000 1 11/2/2011 Poetry 12000 2 12/2/2011 Theatre 5000 3 13/2/2011 Comedy 8000
Using a list of strings
This method involves creating a list of rows (sublists), where each sublist holds string values. Column names are added separately.
Example: This example creates a DataFrame using a list of lists with string values.
import pandas as pd
data = [ ["10/2/2011", "Music", "10000"],
["11/2/2011", "Poetry", "12000"],
["12/2/2011", "Theatre", "5000"],
["13/2/2011", "Comedy", "8000"] ]
df = pd.DataFrame(data, columns=["Date", "Event", "Cost"])
print(df)
Output
Date Event Cost 0 10/2/2011 Music 10000 1 11/2/2011 Poetry 12000 2 12/2/2011 Theatre 5000 3 13/2/2011 Comedy 8000
Using StringIO()
The StringIO() function from Python’s io module treats a string as a file-like object. This allows pd.read_csv() to parse it just like a real CSV file.
Example: This program loads a multi-line string into a DataFrame using StringIO().
import pandas as pd
from io import StringIO
s = StringIO("""Date;Event;Cost
10/2/2011;Music;10000
11/2/2011;Poetry;12000
12/2/2011;Theatre;5000
13/2/2011;Comedy;8000""")
df = pd.read_csv(s, sep=";")
print(df)
Output
Date Event Cost 0 10/2/2011 Music 10000 1 11/2/2011 Poetry 12000 2 12/2/2011 Theatre 5000 3 13/2/2011 Comedy 8000
Using read_clipboard()
The pd.read_clipboard() function lets users copy tabular data and load it directly into a DataFrame. It is convenient but less reliable since it depends on manual copying and clipboard availability.
Example: This example reads string data from the clipboard into a DataFrame.
import pandas as pd
df = pd.read_clipboard(sep=";")
print(df)
Output
Date Event Cost
0 10/2/2011 Music 10000
1 11/2/2011 Poetry 12000
2 12/2/2011 Theatre 5000
3 13/2/2011 Comedy 8000