Home > Backend Development > Python Tutorial > How to Create a Pandas Dataframe from a Dictionary with Arrays of Varying Lengths?

How to Create a Pandas Dataframe from a Dictionary with Arrays of Varying Lengths?

Susan Sarandon
Release: 2024-11-12 21:14:01
Original
631 people have browsed it

How to Create a Pandas Dataframe from a Dictionary with Arrays of Varying Lengths?

Creating Dataframe from Dictionary with Arrays of Varying Lengths

The challenge presented is to generate a dataframe with columns consisting of varying-length numpy array values extracted from a dictionary. To achieve this, let's explore a solution using Python.

In Python 3.x and above, the following code snippet can be employed:

import pandas as pd
import numpy as np

# Define a dictionary with key-value pairs representing numpy arrays
d = {
    "A": np.random.randn(10),
    "B": np.random.randn(12),
    "C": np.random.randn(8)
}

# Create a dataframe by converting each key-value pair to a series
df = pd.DataFrame(
    dict([
        (k, pd.Series(v))
        for k, v in d.items()
    ])
)

# Display the resulting dataframe
print(df)
Copy after login

This code creates a dataframe with columns "A," "B," and "C," each holding corresponding numpy array values from the dictionary. If arrays have varying lengths, it automatically aligns them, extending the shorter arrays with NaN values as padding.

In Python 2.x, a minor modification is required:

import pandas as pd
import numpy as np

# Define a dictionary with key-value pairs representing numpy arrays
d = {
    "A": np.random.randn(10),
    "B": np.random.randn(12),
    "C": np.random.randn(8)
}

# Create a dataframe by converting each key-value pair to a series
df = pd.DataFrame(
    dict([
        (k, pd.Series(v))
        for k, v in d.iteritems()
    ])
)

# Display the resulting dataframe
print(df)
Copy after login

In Python 2.x, the iteritems() function is used instead of items() to iterate over key-value pairs in the dictionary.

By utilizing this approach, you can conveniently create dataframes with columns containing arrays of differing lengths, ensuring that data is properly aligned and handled.

The above is the detailed content of How to Create a Pandas Dataframe from a Dictionary with Arrays of Varying Lengths?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template