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.
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!