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
How to create a DataFrame in Python?
A DataFrame is a 2D data structure in Pandas used to represent data in tabular format with rows and columns. It is similar to a spreadsheet or SQL table and is one of the most important data structures for data analysis in Python.
To create a DataFrame, we need to import pandas. A DataFrame can be created using the DataFrame() constructor function, which accepts data in various formats like dictionaries, lists, or arrays.
Create DataFrame from Dictionary of Lists
When using a dictionary, the keys become column names and values become the data ?
import pandas as pd
data = {'Name': ['Karan', 'Rohit', 'Sahil', 'Aryan'], 'Age': [23, 22, 21, 24]}
df = pd.DataFrame(data)
print(df)
Name Age
0 Karan 23
1 Rohit 22
2 Sahil 21
3 Aryan 24
Create DataFrame from List of Lists
When using a list of lists, you need to specify column names explicitly ?
import pandas as pd data = [['Karan', 23], ['Rohit', 22], ['Sahil', 21], ['Aryan', 24]] df = pd.DataFrame(data, columns=['Name', 'Age']) print(df)
Name Age
0 Karan 23
1 Rohit 22
2 Sahil 21
3 Aryan 24
Create DataFrame with Custom Index
You can specify custom row labels using the index parameter ?
import pandas as pd
data = {'Name': ['Karan', 'Rohit', 'Sahil', 'Aryan'], 'Age': [23, 22, 21, 24]}
df = pd.DataFrame(data, index=['No.1', 'No.2', 'No.3', 'No.4'])
print(df)
Name Age
No.1 Karan 23
No.2 Rohit 22
No.3 Sahil 21
No.4 Aryan 24
Create DataFrame from NumPy Array
You can also create a DataFrame from NumPy arrays ?
import pandas as pd import numpy as np data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) df = pd.DataFrame(data, columns=['A', 'B', 'C']) print(df)
A B C 0 1 2 3 1 4 5 6 2 7 8 9
Comparison of Methods
| Method | Best For | Column Names |
|---|---|---|
| Dictionary of Lists | Named columns with different data types | Automatic (from keys) |
| List of Lists | Uniform data structure | Must specify explicitly |
| NumPy Array | Numerical data | Must specify explicitly |
Conclusion
DataFrames can be created from various data sources including dictionaries, lists, and NumPy arrays. Use dictionaries for labeled data and lists when you need more control over the structure.
