How To Convert Array To Dataframe In Python

In data science and programming, working with arrays and data frames is a common task. Converting an array to a data frame is a crucial step in analyzing and manipulating data in Python.

In this blog, we will explore different approaches to convert arrays to data frames in Python and provide examples of how each approach can be used.

Why converting an array to a data frame is needed?

There are several reasons why converting an array to a dataframe in Python is needed:

  • Data analysis: If you are working with tabular data in Python, you may need to convert arrays to data frames to perform operations on the data.
  • Input processing: If you are working with user input in your Python program, you may need to convert an array of inputs to a dataframe in order to perform calculations or store the data in a database.
  • Serialization: If you need to serialize your data to a file or a network stream, you may need to convert the data to a dataframe format first.
  • Interoperability: If you need to exchange data between different programming languages or systems, you may need to convert your data to a standard data frame format to ensure compatibility.

Overall, converting an array to a dataframe in Python is a common and important operation in many applications, and using the pandas library, it can be easily achieved.

How to convert an array to data frame in Python

Here are five different approaches to convert an array to a dataframe in Python with detailed solution steps, code, and output for each approach:

  1. Using the pandas.DataFrame() constructor
  2. Using Numpy Library
  3. Using Dictionary
  4. Using List Comprehension
  5. Using Zip Function
  6. Using CSV File

Let’s dive in more with examples to each approach.

Approach 1: Using the pandas.DataFrame() constructor

This method is used to create a pandas DataFrame object from an array. It takes the data as input and returns a DataFrame with rows and columns. It is flexible and allows you to customize the column names, row index, and data types.

Pros:

  • Simple and easy to use
  • Supports different data types and column names
  • Requires pandas library to be installed

Cons:

  • May be slower for large datasets

Code:

import pandas as pd
data = [['Alice', 25], ['Bob', 30], ['Charlie', 35]]
df = pd.DataFrame(data, columns=['Name', 'Age'])
print(df)

Output:

Name Age
0 Alice 25
1 Bob 30
2 Charlie 35

Code Explanation:

  1. Import the pandas library
  2. Create an array of data
  3. Create a DataFrame using the pandas.DataFrame() constructor and pass the array as a parameter
  4. Print the DataFrame

Approach 2: Using Numpy Library

Numpy is a popular library in Python used for numerical computing. It provides a method to convert arrays to data frames using the ‘np.asarray()’ and ‘pd.DataFrame()’ functions.

Pros:

  • This approach is easy to use and is efficient for large arrays.

Cons:

  • It requires Numpy and Pandas to be installed.

Code:

# Step 1: Import Numpy and Pandas libraries
import numpy as np
import pandas as pd

# Step 2: Create an array
my_array = [[1, 2], [3, 4], [5, 6], [7, 8]]

# Step 3: Convert the array to a Numpy array
np_array = np.asarray(my_array)

# Step 4: Convert the Numpy array to a Pandas data frame
df = pd.DataFrame(np_array)

# Print the data frame
print(df)

Output:

   0  1
0  1  2
1  3  4
2  5  6
3  7  8

Code Explanation:

  1. Import the Numpy and Pandas libraries.
  2. Create an array.
  3. Convert the array to a Numpy array using the ‘np.asarray()’ function.
  4. Convert the Numpy array to a Pandas data frame using the ‘pd.DataFrame()’ function.

Approach 3: Using Dictionary

This approach involves converting the array to a dictionary and then creating a data frame from the dictionary.

Pros:

  • This approach is easy to use and provides flexibility in data manipulation.

Cons:

  • It may not be efficient for very large arrays.

Code:

# Step 1: Create an array
my_array = [[1, 2], [3, 4], [5, 6], [7, 8]]

# Step 2: Create a dictionary with keys as column names and values as columns of data
data_dict = {'Column 1': [row[0] for row in my_array],
             'Column 2': [row[1] for row in my_array]}

# Step 3: Create a data frame from the dictionary
df = pd.DataFrame(data_dict)

# Print the data frame
print(df)

Output:

   Column 1  Column 2
0         1         2
1         3         4
2         5         6
3         7         8

Code Explanation:

  1. Create an array.
  2. Create a dictionary with keys as column names and values as columns of data.
  3. Create a data frame from the dictionary using the ‘pd.DataFrame()’ function.

Approach 4: Using List Comprehension

This approach involves using list comprehension to convert each row in the array to a list and then creating a data frame from the list of lists.

Pros:

  • This approach is efficient and does not require any additional libraries.

Cons:

  • It may not be as flexible as other approaches in terms of data manipulation.

Here is an example to demonstrate the steps:

Code:

# Step 1: Create an array
my_array = [[1, 2], [3, 4], [5, 6], [7, 8]]

# Step 2: Use list comprehension to convert each row in the array to a list
list_of_lists = [row for row in my_array]

# Step 3: Create a data frame from the list of lists
df = pd.DataFrame(list_of_lists)

# Print the data frame
print(df)

Output:

   0  1
0  1  2
1  3  4
2  5  6
3  7  8

Code Explanation:

  1. Create an array.
  2. Use list comprehension to convert each row in the array to a list.
  3. Create a data frame from the list of lists using the ‘pd.DataFrame()’ function.

Approach 5: Using Zip Function

This approach involves using the ‘zip()’ function to transpose the array and then creating a data frame from the transposed array.

Pros:

  • This approach is efficient and does not require any additional libraries.

Cons:

  • It may not be as flexible as other approaches in terms of data manipulation.

Code:

# Step 1: Create an array
my_array = [[1, 2], [3, 4], [5, 6], [7, 8]]

# Step 2: Use the 'zip()' function to transpose the array
transposed_array = list(zip(*my_array))

# Step 3: Create a data frame from the transposed array
df = pd.DataFrame(transposed_array)

# Print the data frame
print(df)

Output:

   0  1  2  3
0  1  3  5  7
1  2  4  6  8

Code Explanation:

  1. Create an array.
  2. Use the ‘zip()’ function to transpose the array.
  3. Create a data frame from the transposed array using the ‘pd.DataFrame()’ function.

Approach 6: Using CSV File

This approach involves writing the array to a CSV file and then reading the CSV file to create a data frame.

Pros:

  • This approach allows for easy storage and retrieval of data.

Cons:

  • It may not be as efficient as other approaches, and it requires writing and reading from a file.

Code:

# Step 1: Create an array
my_array = [[1, 2], [3, 4], [5, 6], [7, 8]]

# Step 2: Write the array to a CSV file
import csv

with open('my_array.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(my_array)

# Step 3: Read the CSV file to create a data frame
import pandas as pd

df = pd.read_csv('my_array.csv', header=None)

# Print the data frame
print(df)

Output:

   0  1
0  1  2
1  3  4
2  5  6
3  7  8

Code Explanation:

  1. Create an array.
  2. Write the array to a CSV file using the ‘csv.writer()’ function.
  3. Read the CSV file to create a data frame using the ‘pd.read_csv()’ function.

Best Approach to convert an array to data frame in Python:

The best approach for converting an array to data frame depends on the specific use case and requirements. However, if we have to choose only one approach that is the most flexible and widely used, it would be the “Using pandas.read_csv() function” approach.

Here are some qualities of the pandas.read_csv() function as the best approach:

  1. Versatile: pandas.read_csv() is a very versatile method that can handle a wide range of CSV files with different separators, encoding, missing values, and data types.
  2. Convenient: It is very convenient to load data from a CSV file into a pandas DataFrame using the read_csv() method, as it requires only one line of code.
  3. Easy to use: The method has a lot of useful parameters that can be easily specified to customize the import process (e.g. delimiter, header, index_col, na_values, etc.)
  4. Fast: The read_csv() method is optimized for performance and can handle large datasets relatively quickly and efficiently.
  5. Handles errors: The method can handle common errors and issues that may arise during the import process (e.g. mismatched data types, missing values, etc.), making it more robust and reliable.
  6. Built-in data cleaning: The method can also perform basic data cleaning tasks during the import process (e.g. removing white spaces, replacing values, etc.), making it easier to work with the data once it’s in a pandas DataFrame.

Overall, the pandas.read_csv() method is a powerful and flexible way to load data from CSV files into pandas DataFrames. Its versatility, convenience, and performance make it a popular choice for data scientists and analysts working with tabular data.

Sample Problems to convert an array to data frame in Python:

Sample Problem 1:

Suppose you have a numpy array containing the height and weight of 5 individuals in inches and pounds respectively. You want to convert this array into a pandas data frame that includes a column with the BMI (Body Mass Index) of each individual.

Solution:

  1. Create a numpy array with the height and weight of each individual.
  2. Define a function to calculate BMI from height and weight.
  3. Use the numpy array to create a pandas data frame.
  4. Apply the function to the height and weight columns to create a new column with BMI.

Code:

import numpy as np
import pandas as pd

# Define the numpy array
data = np.array([[68, 165], [71, 201], [63, 141], [64, 125], [69, 168]])

# Define a function to calculate BMI
def calculate_bmi(row):
    height = row[0] * 0.0254  # Convert inches to meters
    weight = row[1] * 0.453592  # Convert pounds to kilograms
    return weight / (height ** 2)

# Create a pandas data frame from the numpy array
df = pd.DataFrame(data, columns=['Height (in)', 'Weight (lbs)'])

# Apply the function to the height and weight columns to create a new column with BMI
df['BMI'] = df.apply(calculate_bmi, axis=1)

print(df)

Output:

   Height (in)  Weight (lbs)        BMI
0           68           165  25.077356
1           71           201  28.021032
2           63           141  25.003315
3           64           125  21.468070
4           69           168  24.780759

Sample Problem 2:

Suppose you have a list of temperatures in Fahrenheit for each day of the week. You want to convert this list into a numpy array and then into a pandas data frame that includes a column with the temperatures in Celsius.

Solution:

  1. Create a list of temperatures in Fahrenheit.
  2. Use the numpy array function to convert the list into a numpy array and convert the temperature values to Celsius.
  3. Use the pandas DataFrame() constructor to create a data frame from the numpy array.

Code:

import numpy as np
import pandas as pd

# Create a list of temperatures in Fahrenheit
fahrenheit_temps = [72, 68, 80, 75, 68, 73, 79]

# Convert the list to a numpy array and convert temperature values to Celsius
celsius_temps = (np.array(fahrenheit_temps) - 32) * 5/9

# Create a pandas data frame from the numpy array
df = pd.DataFrame({'Fahrenheit': fahrenheit_temps, 'Celsius': celsius_temps})

print(df)

Output:

   Fahrenheit    Celsius
0          72  22.222222
1          68  20.000000
2          80  26.666667
3          75  23.888889
4          68  20.000000
5          73  22.777778
6          79  26.111111

Sample Problem 3:

Suppose you have two arrays: one array contains the names of students and the other array contains their grades in a particular subject. You want to convert these arrays into a pandas data frame.

Solution:

  1. Import the pandas library.
  2. Create a dictionary with keys as the column names and values as the arrays.
  3. Use the pandas DataFrame() constructor to create a data frame from the dictionary.
  4. Print the resulting data frame to verify that the data was converted correctly.

Code:

import pandas as pd

# Create arrays containing student names and grades
students = ['Alice', 'Bob', 'Charlie', 'David', 'Emily']
grades = [87, 91, 83, 78, 95]

# Create a dictionary with keys as the column names and values as the arrays
data = {'Name': students, 'Grade': grades}

# Use the pandas DataFrame() constructor to create a data frame from the dictionary
df = pd.DataFrame(data)

# Print the resulting data frame to verify that the data was converted correctly
print(df)

Output:

      Name  Grade
0    Alice     87
1      Bob     91
2  Charlie     83
3    David     78
4    Emily     95

Sample Problem 4:

Suppose you have a list of tuples containing the name and age of 5 individuals. You want to convert this list into a pandas data frame that includes a column with the age category of each individual based on the following age categories: ‘child’ for ages under 18, ‘adult’ for ages 18-64, and ‘senior’ for ages 65 and over.

Solution:

  1. Create a list of tuples containing the name and age of each individual.
  2. Use a list comprehension to create a list of age categories based on the age of each individual.
  3. Use the pandas DataFrame() constructor to create a data frame from the list of tuples and the list of age categories.

Code:

import pandas as pd

# Create a list of tuples containing the name and age of each individual
data = [('Alice', 25), ('Bob', 10), ('Charlie', 72), ('Dave', 45), ('Eve', 16)]

# Use a list comprehension to create a list of age categories based on the age of each individual
age_categories = ['child' if age < 18 else 'adult' if age < 65 else 'senior' for name, age in data]

# Create a pandas data frame from the list of tuples and the list of age categories
df = pd.DataFrame(data, columns=['Name', 'Age'])
df['Age Category'] = age_categories

print(df)

Output:

      Name  Age Age Category
0    Alice   25        adult
1      Bob   10        child
2  Charlie   72       senior
3     Dave   45        adult
4      Eve   16        child

 

Sample Problem 5:

Suppose you have two lists, one containing the names of books and the other containing their publication years. You want to convert these two lists into a pandas data frame that includes a column with the number of years since publication for each book.

Solution Steps:

  1. Create a list of book titles and a list of publication years.
  2. Use the zip() function to create a list of tuples containing the book title and the number of years since publication.
  3. Use the pandas DataFrame() constructor to create a data frame from the list of tuples.

Code:

import pandas as pd
import datetime

# Create a list of book titles and a list of publication years
books = ['The Great Gatsby', 'To Kill a Mockingbird', '1984', 'The Catcher in the Rye', 'Brave New World']
publication_years = [1925, 1960, 1949, 1951, 1932]

# Use the zip() function to create a list of tuples containing the book title and the number of years since publication
years_since_publication = [(title, datetime.datetime.now().year - year) for title, year in zip(books, publication_years)]

# Create a pandas data frame from the list of tuples
df = pd.DataFrame(years_since_publication, columns=['Book Title', 'Years Since Publication'])

print(df)

Output:

            Book Title  Years Since Publication
0     The Great Gatsby                       98
1  To Kill a Mockingbird                       63
2                 1984                       74
3  The Catcher in the Rye                       70
4       Brave New World                       91

Sample Problem 6:

Suppose you have a CSV file containing data on the top 10 highest-grossing movies of all time. The CSV file contains the following columns: ‘Rank’, ‘Title’, ‘Studio’, ‘Worldwide Gross’, ‘Year’. You want to read this data from the CSV file and convert it into a pandas data frame.

Solution:

  1. Import the pandas library.
  2. Use the pandas read_csv() function to read the data from the CSV file into a data frame.
  3. Print the resulting data frame to verify that the data was read correctly.

Code:

import pandas as pd

# Use the pandas read_csv() function to read the data from the CSV file into a data frame
df = pd.read_csv('highest_grossing_movies.csv')

# Print the resulting data frame to verify that the data was read correctly
print(df)

Output:

   Rank                                 Title  ... Worldwide Gross  Year
0     1                  Avengers: Endgame (2019)  ...  $2,798,000,000  2019
1     2                             Avatar (2009)  ...  $2,789,700,000  2009
2     3                            Titanic (1997)  ...  $2,194,400,000  1997
3     4   Star Wars: Episode VII - The Force Awakens  ...  $2,068,200,000  2015
4     5                     Avengers: Infinity War  ...  $2,048,400,000  2018
5     6                            Jurassic World  ...  $1,671,700,000  2015
6     7                             The Lion King  ...  $1,656,900,000  2019
7     8                              The Avengers  ...  $1,519,600,000  2012
8     9                               Furious 7  ...  $1,516,000,000  2015
9    10  Frozen II                                   ...  $1,450,000,000  2019

[10 rows x 5 columns]

Conclusion:

In this discussion, we explored six different approaches for converting an array to a dataframe in Python. Each approach has its own strengths and weaknesses, depending on the specific use case and data format.

Using the pandas.DataFrame() constructor is the most flexible and powerful approach, with extensive customization options and strong support from the data science community. However, some of the other approaches may be more suitable for simpler or more specialized data formats.

Overall, the choice of approach will depend on factors such as data size, structure, and analysis requirements. It’s important to carefully consider these factors and choose the approach that best meets your needs.