Adding NumPy Array as a New Column in Pandas DataFrame

Pandas is a widely popular Python library for data manipulation and analysis. NumPy is a Python library that provides support for arrays and matrices.

Both libraries are powerful in their respective areas and can be used together to perform advanced data analysis. In this article, we will explore how to add NumPy arrays as new columns in Pandas DataFrames.

Example 1: Adding NumPy array as new column in DataFrame

One of the most basic operations in data analysis is to add a new column to a DataFrame. In this example, we will create a NumPy array and use it to create a new column in an existing DataFrame.

First, we import the necessary libraries:

``````import pandas as pd
import numpy as np``````

Next, we create a Pandas DataFrame:

``````df = pd.DataFrame({'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]})``````

This creates a DataFrame with three columns: ‘A’, ‘B’, and ‘C’, and three rows. We can print the DataFrame to see its contents:

``print(df)``

Output:

``````   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9``````

Now, we create a NumPy array:

``new_column = np.array([10, 11, 12])``

This creates a 1-dimensional NumPy array with three elements: 10, 11, and 12. We can now append this array as a new column in our DataFrame:

``df['D'] = new_column``

This adds a new column named ‘D’ to our DataFrame, with the values from the NumPy array.

We can print our DataFrame again to see the new column:

``print(df)``

Output:

``````   A  B  C   D
0  1  4  7  10
1  2  5  8  11
2  3  6  9  12``````

Example 2: Adding NumPy matrix as new columns in DataFrame

In the previous example, we added a 1-dimensional NumPy array as a new column to a Pandas DataFrame. In this example, we will show how to add a 2-dimensional NumPy matrix as new columns in a Pandas DataFrame.

First, we create a NumPy matrix:

``````new_columns = np.array([[13, 14],
[15, 16],
[17, 18]])``````

This creates a 2-dimensional NumPy matrix with three rows and two columns: 13, 14 in the first row, 15, 16 in the second row, and 17, 18 in the third row. Now, we create a Pandas DataFrame:

``````df = pd.DataFrame({'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]})``````

This creates a DataFrame with three columns: ‘A’, ‘B’, and ‘C’, and three rows.

We can print the DataFrame to see its contents:

``print(df)``

Output:

``````   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9``````

Now, we use the `concat` method of Pandas to concatenate the original DataFrame with the NumPy matrix along axis 1. We also use the `rename` method to give names to the new columns:

``new_df = pd.concat([df, pd.DataFrame(data=new_columns, columns=['D', 'E'])], axis=1)``

This creates a new DataFrame `new_df`, with five columns: ‘A’, ‘B’, ‘C’, ‘D’, and ‘E’.

The new columns ‘D’ and ‘E’ have the values from the NumPy matrix. We can print our new DataFrame to see its contents:

``print(new_df)``

Output:

``````   A  B  C   D   E
0  1  4  7  13  14
1  2  5  8  15  16
2  3  6  9  17  18``````

If you want to learn more about Pandas and NumPy, there are many resources available online. Here are a few that we recommend:

Conclusion

In this article, we showed how to add NumPy arrays and matrices as new columns in Pandas DataFrames. Adding new columns is a basic operation that is often used in data analysis and can be done easily using Pandas and NumPy. We hope that this article has been informative and helpful in your data analysis endeavors.

In conclusion, this article discussed how to add NumPy arrays and matrices as new columns in Pandas DataFrames. Data analysis frequently involves adding new columns, and this can be easily accomplished using the Pandas and NumPy libraries.

The article provided two examples of adding new columns using these libraries, along with additional resources for further learning. It is important for data analysts to be proficient in these libraries and understand how they work together to achieve advanced data analysis.

With the knowledge gained from this article, readers can now utilize Pandas and NumPy to enhance their data analysis projects.