In this guide, we will learn how to create a Pandas DataFrame from lists. Pandas is a powerful library for data manipulation and analysis in Python, and it provides a convenient way to work with tabular data using DataFrames.
Creating a DataFrame
To create a DataFrame from lists, we can use the pd.DataFrame()
function provided by the Pandas library. This function takes a dictionary as an argument, where the keys represent column names and the values represent the corresponding lists of data.
Let’s take a look at an example:
import pandas as pd Create lists of data names = ['Alice', 'Bob', 'Charlie', 'Dave'] ages = [25, 30, 35, 40] cities = ['New York', 'Los Angeles', 'Chicago', 'Houston'] Create a dictionary from the lists data = {'Name': names, 'Age': ages, 'City': cities} Create a DataFrame from the dictionary df = pd.DataFrame(data) Print the DataFrame print(df)
Name Age City 0 Alice 25 New York 1 Bob 30 Los Angeles 2 Charlie 35 Chicago 3 Dave 40 Houston
As you can see, we first created three separate lists for the names, ages, and cities. Then, we created a dictionary called data
with the keys ‘Name’, ‘Age’, and ‘City’ corresponding to the column names. Finally, we passed the data
dictionary to the pd.DataFrame()
function to create the DataFrame.
Method 2: Using a dictionary
A dictionary is another data structure that can be used to create a DataFrame. In this case, the keys of the dictionary will be the column names of the DataFrame, and the values will be the lists that represent the data in each column.
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 40], 'Salary': [50000, 60000, 70000, 80000]} df = pd.DataFrame(data) print(df)
Output:
Name Age Salary 0 Alice 25 50000 1 Bob 30 60000 2 Charlie 35 70000 3 David 40 80000
In this example, we define a dictionary with three keys (Name, Age, and Salary), and the corresponding values are lists that contain the data for each column. We then pass this dictionary to the DataFrame constructor to create the DataFrame.
Method 3: Using a list of dictionaries
Another way to create a DataFrame is to use a list of dictionaries. Each dictionary in the list represents a row in the DataFrame, and the keys of the dictionaries correspond to the column names.
import pandas as pd data = [{'Name': 'Alice', 'Age': 25, 'Salary': 50000}, {'Name': 'Bob', 'Age': 30, 'Salary': 60000}, {'Name': 'Charlie', 'Age': 35, 'Salary': 70000}, {'Name': 'David', 'Age': 40, 'Salary': 80000}] df = pd.DataFrame(data) print(df)
Output:
Name Age Salary 0 Alice 25 50000 1 Bob 30 60000 2 Charlie 35 70000 3 David 40 80000
In this example, we define a list of dictionaries, where each dictionary represents a row in the DataFrame. We then pass this list to the DataFrame constructor to create the DataFrame.
Last Updated on May 17, 2023 by admin