Making a Pandas DataFrame with two-dimensional list



In this article, we will explore how to create a Pandas DataFrame using a two-dimensional list in Python.
Pandas is a powerful library for data manipulation and analysis, and the DataFrame is one of its core data
structures.

Creating a DataFrame

To create a DataFrame from a two-dimensional list, we can use the pd.DataFrame() function provided by
Pandas. Let’s take a look at an example:

import pandas as pd

data = [['Alice', 25, 'Engineer'],
['Bob', 30, 'Developer'],
['Charlie', 35, 'Manager']]

df = pd.DataFrame(data, columns=['Name', 'Age', 'Job'])
print(df)

Output:

  Name  Age        Job

0 Alice 25 Engineer
1 Bob 30 Developer
2 Charlie 35 Manager

In Above code, we first import the Pandas library using the import statement. Then, we define our
data as a two-dimensional list called data. Each inner list represents a row in the DataFrame, and
each element within the inner list represents a column value.

We then pass the data list and the column names as arguments to the pd.DataFrame()
function, which creates the DataFrame. Finally, we use the print() function to display the
DataFrame.

Example:

import pandas as pd

# Define the two-dimensional list
data = [['Alice', 25, 'Engineer'],
        ['Bob', 30, 'Developer'],
        ['Charlie', 35, 'Manager']]

# Create a DataFrame
df = pd.DataFrame(data, columns=['Name', 'Age', 'Job'])

# Print the DataFrame
print(df)

Output:

      Name  Age        Job
0    Alice   25   Engineer
1      Bob   30  Developer
2  Charlie   35    Manager

 

Method 1: Using a Dictionary to Define Column Values

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'Job': ['Engineer', 'Developer', 'Manager']}

df = pd.DataFrame(data)
print(df)

Output:

      Name  Age        Job
0    Alice   25   Engineer
1      Bob   30  Developer
2  Charlie   35    Manager

Method 2: Using List Comprehension

import pandas as pd

data = [['Alice', 25, 'Engineer'],
        ['Bob', 30, 'Developer'],
        ['Charlie', 35, 'Manager']]

columns = ['Name', 'Age', 'Job']

df = pd.DataFrame([dict(zip(columns, row)) for row in data])
print(df)

Output:

      Name  Age        Job
0    Alice   25   Engineer
1      Bob   30  Developer
2  Charlie   35    Manager

Method 3: Using the from_records() Method

import pandas as pd

data = [['Alice', 25, 'Engineer'],
        ['Bob', 30, 'Developer'],
        ['Charlie', 35, 'Manager']]

columns = ['Name', 'Age', 'Job']

df = pd.DataFrame.from_records(data, columns=columns)
print(df)

Output:

      Name  Age        Job
0    Alice   25   Engineer
1      Bob   30  Developer
2  Charlie   35    Manager

Method 4: Using the append() Method

import pandas as pd

data = [['Alice', 25, 'Engineer'],
        ['Bob', 30, 'Developer'],
        ['Charlie', 35, 'Manager']]

columns = ['Name', 'Age', 'Job']

df = pd.DataFrame(columns=columns)

for row in data:
    df = df.append(pd.Series(row, index=columns), ignore_index=True)

print(df)


Output:

      Name Age        Job
0    Alice  25   Engineer
1      Bob  30  Developer
2  Charlie  35    Manager

Method 5: Using the DataFrame constructor with zip()

import pandas as pd

data = [['Alice', 25, 'Engineer'],
        ['Bob', 30, 'Developer'],
        ['Charlie', 35, 'Manager']]

columns = ['Name', 'Age', 'Job']

df = pd.DataFrame(list(zip(*data)), columns=columns)

print(df)


Output:

      Name Age        Job
0    Alice  25   Engineer
1      Bob  30  Developer
2  Charlie  35    Manager

Last Updated on May 17, 2023 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs