A Comprehensive Guide on How to Create Pandas Dataframe: 10 Effective Methods
Pandas is a powerful data manipulation library in Python that provides various data structures and functions to efficiently handle and analyze data. One of the most commonly used data structures in Pandas is the DataFrame, which is a two-dimensional table-like structure that stores data in rows and columns. In this article, we will explore 10 effective methods to create a Pandas DataFrame.
1. Creating a DataFrame from a Dictionary:
One of the simplest ways to create a DataFrame is by using a dictionary. Each key-value pair in the dictionary represents a column name and its corresponding values. We can pass this dictionary to the `pd.DataFrame()` function to create a DataFrame.
“`python
import pandas as pd
data = {‘Name’: [‘John’, ‘Emma’, ‘Michael’],
‘Age’: [25, 30, 35],
‘City’: [‘New York’, ‘London’, ‘Paris’]}
df = pd.DataFrame(data)
“`
2. Creating a DataFrame from a List of Lists:
Another method is to create a DataFrame from a list of lists. Each inner list represents a row in the DataFrame, and the outer list contains all the rows. We can pass this list to the `pd.DataFrame()` function.
“`python
import pandas as pd
data = [[‘John’, 25, ‘New York’],
[‘Emma’, 30, ‘London’],
[‘Michael’, 35, ‘Paris’]]
df = pd.DataFrame(data, columns=[‘Name’, ‘Age’, ‘City’])
“`
3. Creating a DataFrame from a CSV file:
Pandas provides a convenient method to read data from CSV files and create a DataFrame. We can use the `pd.read_csv()` function and specify the file path as an argument.
“`python
import pandas as pd
df = pd.read_csv(‘data.csv’)
“`
4. Creating an Empty DataFrame:
We can create an empty DataFrame with just the column names specified. This can be useful when we want to add data later on.
“`python
import pandas as pd
df = pd.DataFrame(columns=[‘Name’, ‘Age’, ‘City’])
“`
5. Creating a DataFrame from a NumPy array:
If we have data stored in a NumPy array, we can convert it into a DataFrame using the `pd.DataFrame()` function.
“`python
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’])
“`
6. Creating a DataFrame from a Series:
A Series is a one-dimensional labeled array in Pandas. We can create a DataFrame from a Series by passing it to the `pd.DataFrame()` function.
“`python
import pandas as pd
series = pd.Series([10, 20, 30])
df = pd.DataFrame(series, columns=[‘Numbers’])
“`
7. Creating a DataFrame from a Dictionary of Series:
We can also create a DataFrame from a dictionary of Series. Each key-value pair represents a column name and its corresponding Series.
“`python
import pandas as pd
data = {‘Name’: pd.Series([‘John’, ‘Emma’, ‘Michael’]),
‘Age’: pd.Series([25, 30, 35]),
‘City’: pd.Series([‘New York’, ‘London’, ‘Paris’])}
df = pd.DataFrame(data)
“`
8. Creating a DataFrame from a List of Dictionaries:
If we have a list of dictionaries, where each dictionary represents a row in the DataFrame, we can pass this list to the `pd.DataFrame()` function.
“`python
import pandas as pd
data = [{‘Name’: ‘John’, ‘Age’: 25, ‘City’: ‘New York’},
{‘Name’: ‘Emma’, ‘Age’: 30, ‘City’: ‘London’},
{‘Name’: ‘Michael’, ‘Age’: 35, ‘City’: ‘Paris’}]
df = pd.DataFrame(data)
“`
9. Creating a DataFrame from an Excel file:
Similar to CSV files, Pandas also provides a method to read data from Excel files and create a DataFrame. We can use the `pd.read_excel()` function and specify the file path as an argument.
“`python
import pandas as pd
df = pd.read_excel(‘data.xlsx’)
“`
10. Creating a DataFrame from a SQL query:
Pandas allows us to connect to databases and execute SQL queries to fetch data and create a DataFrame. We can use the `pd.read_sql_query()` function and pass the
- SEO Powered Content & PR Distribution. Get Amplified Today.
- PlatoData.Network Vertical Generative Ai. Empower Yourself. Access Here.
- PlatoAiStream. Web3 Intelligence. Knowledge Amplified. Access Here.
- PlatoESG. Carbon, CleanTech, Energy, Environment, Solar, Waste Management. Access Here.
- PlatoHealth. Biotech and Clinical Trials Intelligence. Access Here.
- Source: Plato Data Intelligence.
- Source Link: https://zephyrnet.com/10-ways-to-create-pandas-dataframe/