This tutorial will show how you can combine multiple arrays (e.g., 2 arrays of X and Y) into a Pandas dataframe. The following summarizes the two methods.
Method 1:
pd.DataFrame ({‘X’:X,’Y’:Y})
Method 2:
combined_array=np.column_stack((X,Y))
pd.DataFrame(combined_array, columns = [‘X’,’Y’])
Two Examples of Combining Arrays into Dataframe
Example for Method 1:
In the following, we create two arrays, X and Y. Then, we combine them into a pandas dataframe.
# Import numpy and pandas
import numpy as np
import pandas as pd
# Create two Numpy arrays, X and Y
X = np.array([5, 2, 3, 4, 10, 11, 14])
Y = np.array([3, 1, 2, 5, 14, 15, 16])
# combine two arrays into a dataframe and print it out
df_1 = pd.DataFrame ({'X':X,'Y':Y})
print (df_1)
Output:
X Y 0 5 3 1 2 1 2 3 2 3 4 5 4 10 14 5 11 15 6 14 16
Example for Method 2:
We can use np.column_stack() to combine two 1-D arrays X and Y into a 2-D array. Then, we can use pd.DataFrame
to change it into a dataframe.
# Import numpy and pandas
import numpy as np
import pandas as pd
# Create two Numpy arrays, X and Y
X = np.array([5, 2, 3, 4, 10, 11, 14])
Y = np.array([3, 1, 2, 5, 14, 15, 16])
# combine two 1-D arrays into a single 2-D array
combined_array=np.column_stack((X,Y))
print("combined array:\n", combined_array)
# combine two arrays into a dataframe and print it out
df_2 = pd.DataFrame(combined_array, columns = ['X','Y'])
print("combined dataframe:\n",df_2)
Output:
combined array: [[ 5 3] [ 2 1] [ 3 2] [ 4 5] [10 14] [11 15] [14 16]] combined dataframe: X Y 0 5 3 1 2 1 2 3 2 3 4 5 4 10 14 5 11 15 6 14 16