"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Published on 2024-11-11
Browse:359

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)

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)

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3