10 Different Ways to Create Pandas DataFrame | Python Data Analysis

Published: 26 July 2023
on channel: sumit kumar
158
14

In this video, we'll explore ten different methods to create a Pandas DataFrame in Python. Pandas is a powerful library for data manipulation and analysis, and understanding these various techniques will boost your data handling skills. Whether you prefer dictionaries, lists, CSV files, or other data sources, we've got you covered! Watch the video to learn how to create DataFrames effortlessly using Pandas.

#From a Dictionary:
import pandas as pd

Example data in dictionary format
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']
}

Create DataFrame from dictionary
df = pd.DataFrame(data)

print(df)
--------------------------------------
2)From a List of Lists:
import pandas as pd

Example data in list of lists format
data = [
['Alice', 25, 'New York'],
['Bob', 30, 'London'],
['Charlie', 35, 'Paris']
]

Create DataFrame from list of lists
df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])

print(df)

-------------------
3)From a CSV File:
import pandas as pd

Read data from a CSV file
df = pd.read_csv('data.csv')

print(df)
----------
4)From a List of Dictionaries:
import pandas as pd

Example data in list of dictionaries format
data = [
{'Name': 'Alice', 'Age': 25, 'City': 'New York'},
{'Name': 'Bob', 'Age': 30, 'City': 'London'},
{'Name': 'Charlie', 'Age': 35, 'City': 'Paris'}
]

Create DataFrame from list of dictionaries
df = pd.DataFrame(data)
5)
From a List of Tuples:
import pandas as pd

Example data in list of tuples format
data = [
('Alice', 25, 'New York'),
('Bob', 30, 'London'),
('Charlie', 35, 'Paris')
]

Create DataFrame from list of tuples
df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])

print(df)
-----------
6)From a NumPy Array:
import pandas as pd
import numpy as np

Example data in NumPy array format
data = np.array([
['Alice', 25, 'New York'],
['Bob', 30, 'London'],
['Charlie', 35, 'Paris']
])

Create DataFrame from NumPy array
df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])

print(df)
7)
From a Dictionary of Series:
import pandas as pd

Example data in dictionary of Series format
data = {
'Name': pd.Series(['Alice', 'Bob', 'Charlie']),
'Age': pd.Series([25, 30, 35]),
'City': pd.Series(['New York', 'London', 'Paris'])
}

Create DataFrame from dictionary of Series
df = pd.DataFrame(data)

print(df)
8)From a NumPy structured array:
import pandas as pd
import numpy as np

Example data in NumPy structured array format
data = np.array([
('Alice', 25, 'New York'),
('Bob', 30, 'London'),
('Charlie', 35, 'Paris')
], dtype=[('Name', 'U10'), ('Age', int), ('City', 'U10')])

Create DataFrame from NumPy structured array
df = pd.DataFrame(data)

print(df)

9)
From a database query (using pandas' database connectors):
import pandas as pd
import sqlite3

Connect to the SQLite database
conn = sqlite3.connect('example.db')

Query data from the database and create DataFrame
query = "SELECT * FROM table_name;"
df = pd.read_sql(query, conn)

print(df)


On this page of the site you can watch the video online 10 Different Ways to Create Pandas DataFrame | Python Data Analysis with a duration of hours minute second in good quality, which was uploaded by the user sumit kumar 26 July 2023, share the link with friends and acquaintances, this video has already been watched 158 times on youtube and it was liked by 14 viewers. Enjoy your viewing!