make dataframe out of two np arrays in python

To make a dataframe out of two NumPy arrays in Python, you can use the pandas.DataFrame() constructor. Here's an example:

main.py
import numpy as np
import pandas as pd

array1 = np.array([1, 2, 3, 4])
array2 = np.array(['A', 'B', 'C', 'D'])

df = pd.DataFrame({'Column1': array1, 'Column2': array2})

print(df)
182 chars
10 lines

In the above code, two NumPy arrays are initialized. Then, the pandas.DataFrame() constructor is used with a dictionary to create a dataframe with two columns, 'Column1' and 'Column2', containing the values of array1 and array2, respectively. The resulting dataframe is then printed to the console:

main.py
   Column1 Column2
0        1      A
1        2      B
2        3      C
3        4      D
91 chars
6 lines

You can replace array1 and array2 with your own arrays in the code snippet above to create your own dataframe.

related categories

gistlibby LogSnag