🧱 Pandas Creating and Accessing DataFrames – Build & Retrieve Tabular Data Easily
🧲 Introduction – Why Create and Access DataFrames?
Pandas DataFrames are essential when dealing with structured data such as spreadsheets, CSV files, or database tables. Knowing how to create and access DataFrames efficiently is the foundation of every data analysis workflow in Python.
🎯 In this guide, you’ll learn:
- How to create DataFrames from dictionaries, lists, and arrays
- Ways to access columns, rows, and specific cells
- Label-based vs position-based indexing (
.loc[]vs.iloc[]) - Real-world scenarios for accessing and manipulating tabular data
🛠️ 1. Create a DataFrame from a Dictionary
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [24, 27, 22],
'Score': [88.5, 92.3, 79.8]
}
df = pd.DataFrame(data)
print(df)
👉 Output:
Name Age Score
0 Alice 24 88.5
1 Bob 27 92.3
2 Charlie 22 79.8
✅ Each dictionary key becomes a column; each list is a column’s values.
📋 2. Create DataFrame from List of Lists
data = [['Alice', 24, 88.5], ['Bob', 27, 92.3]]
df = pd.DataFrame(data, columns=['Name', 'Age', 'Score'])
print(df)
👉 Output:
Name Age Score
0 Alice 24 88.5
1 Bob 27 92.3
✅ Useful when your data is in row-major order.
🔗 3. Create DataFrame from List of Dictionaries
data = [{'Name': 'Alice', 'Age': 24}, {'Name': 'Bob', 'Age': 27, 'Score': 92}]
df = pd.DataFrame(data)
print(df)
👉 Output:
Name Age Score
0 Alice 24 NaN
1 Bob 27 92.0
✅ Missing values are filled with NaN by default.
🧪 4. Access Columns
print(df['Name']) # Single column as Series
print(df[['Name', 'Age']]) # Multiple columns as DataFrame
✅ Columns are accessed like keys in a dictionary.
🔍 5. Access Rows by Index
➕ Using .loc[] (Label-based)
print(df.loc[0]) # First row
➕ Using .iloc[] (Position-based)
print(df.iloc[1]) # Second row
✅ Use .loc[] when working with custom indices and .iloc[] for default integer positions.
✂️ 6. Access Specific Cell
print(df.loc[0, 'Score']) # By label
print(df.iloc[1, 2]) # By position
✅ Both .loc[] and .iloc[] support 2D selection: [row, column]
🎯 7. Slice Rows
print(df[0:2]) # First two rows
print(df.iloc[1:]) # From second row to end
✅ Slicing syntax is like Python lists
🧾 8. Add or Modify Columns
df['Passed'] = df['Score'] > 80
print(df)
👉 Output:
Name Age Score Passed
0 Alice 24 88.5 True
1 Bob 27 92.3 True
2 Charlie 22 79.8 False
✅ Operations apply element-wise and create new columns dynamically.
📌 Summary – Recap & Next Steps
Creating and accessing Pandas DataFrames lets you quickly work with structured tabular data, extract insights, and prepare it for deeper analysis or visualization.
🔍 Key Takeaways:
- DataFrames can be created from dicts, lists, or structured formats like CSV
- Use
[],.loc[], and.iloc[]to access and slice data - Modify DataFrames by assigning values to new or existing columns
- Combine row/column access for precise data manipulation
⚙️ Real-world relevance: Core to data cleaning, analysis, and feature extraction in machine learning, finance, healthcare, and web analytics.
❓ FAQs – Creating and Accessing DataFrames
❓ What’s the difference between .loc[] and .iloc[]?
✅ .loc[] accesses by label, .iloc[] accesses by integer position.
❓ Can I create a DataFrame without column names?
✅ Yes, but column names will default to integers unless specified.
❓ How do I access a single cell from a DataFrame?
Use:
df.loc[row_label, 'ColumnName']
or
df.iloc[row_index, column_index]
❓ Can I modify a row or column directly?
✅ Yes, DataFrames are mutable:
df.at[0, 'Score'] = 90
❓ Can I create a DataFrame with mixed data types?
✅ Yes, each column can have its own data type (e.g., int, float, string).
Share Now :
