How to append columns in Pandas

In this article, you will learn about how to append columns in Pandas. A Pandas DataFrame is nothing but a two-dimensional data structure or two-dimensional…


This content originally appeared on CodeSource.io and was authored by Md Niaz Rahman Khan

In this article, you will learn about how to append columns in Pandas.

A Pandas DataFrame is nothing but a two-dimensional data structure or two-dimensional array that represents the data in rows and columns. It is open-source and potent, fast, and easy to use. Basically, while working with big data we need to analyze, manipulate and update them and the pandas’ library plays a lead role there. In Pandas, we may need to append a column. Here, append means adding a new column to the existing one. To perform this action, Pandas provide a method named append(). This method will help us to append columns in Pandas. In this article, we will explore this method and see how we can append columns in Pandas. Before doing so, let’s create a simple Pandas DataFrame in the below section:

import pandas as pd

student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John'],
                           'Age' : ['21', '23', '19']
                          })
student_df2 = pd.DataFrame({'Name' : ['Sam', 'Billy', 'Alia'],
                           'Age' : ['20', '18', '22']
                          })

print(student_df)
print(student_df2)

# Output: 
#     Name Age
# 0   Alex  21
# 1  Deven  23
# 2   John  19
#     Name Age
# 0    Sam  20
# 1  Billy  18
# 2   Alia  22

Here, you can see that we have two students DataFrame. You may ask why we have created two different DataFrame while the column names are the same. Let’s imagine the scenario, at first, we work with some students. Later on, a few more students arrived. Now, we need to work them together. In the next section, we will see the process of performing this action.

Example code: append columns in Pandas using append() method

We will use the append() method to perform this action in Pandas. This method will simply append the second DataFrame column right after the first one. Follow the below code example to understand it more clearly:

import pandas as pd

student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John'],
                           'Age' : ['21', '23', '19']
                          })
student_df2 = pd.DataFrame({'Name' : ['Sam', 'Billy', 'Alia'],
                           'Age' : ['20', '18', '22']
                          })
append_df = student_df.append(student_df2)
print(append_df)


# Output: 
#     Name Age
# 0   Alex  21
# 1  Deven  23
# 2   John  19
# 0    Sam  20
# 1  Billy  18
# 2   Alia  22

Here, you can see that the second DataFrame column has been successfully appended right after the first DataFrame. But you may notice that the index number is not in the correct format. To fix this see the below code example:

import pandas as pd

student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John'],
                           'Age' : ['21', '23', '19']
                          })
student_df2 = pd.DataFrame({'Name' : ['Sam', 'Billy', 'Alia'],
                           'Age' : ['20', '18', '22']
                          })
append_df = student_df.append(student_df2, ignore_index = True)
print(append_df)


# Output: 
#     Name Age
# 0   Alex  21
# 1  Deven  23
# 2   John  19
# 3    Sam  20
# 4  Billy  18
# 5   Alia  22

This time you can see that the index number has been set in the proper format. The reason behind this is that we set ignore_index = True. This is all about append columns in Pandas. Finally, you may follow this article to perform this action.


This content originally appeared on CodeSource.io and was authored by Md Niaz Rahman Khan


Print Share Comment Cite Upload Translate Updates
APA

Md Niaz Rahman Khan | Sciencx (2022-10-03T09:52:31+00:00) How to append columns in Pandas. Retrieved from https://www.scien.cx/2022/10/03/how-to-append-columns-in-pandas/

MLA
" » How to append columns in Pandas." Md Niaz Rahman Khan | Sciencx - Monday October 3, 2022, https://www.scien.cx/2022/10/03/how-to-append-columns-in-pandas/
HARVARD
Md Niaz Rahman Khan | Sciencx Monday October 3, 2022 » How to append columns in Pandas., viewed ,<https://www.scien.cx/2022/10/03/how-to-append-columns-in-pandas/>
VANCOUVER
Md Niaz Rahman Khan | Sciencx - » How to append columns in Pandas. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/03/how-to-append-columns-in-pandas/
CHICAGO
" » How to append columns in Pandas." Md Niaz Rahman Khan | Sciencx - Accessed . https://www.scien.cx/2022/10/03/how-to-append-columns-in-pandas/
IEEE
" » How to append columns in Pandas." Md Niaz Rahman Khan | Sciencx [Online]. Available: https://www.scien.cx/2022/10/03/how-to-append-columns-in-pandas/. [Accessed: ]
rf:citation
» How to append columns in Pandas | Md Niaz Rahman Khan | Sciencx | https://www.scien.cx/2022/10/03/how-to-append-columns-in-pandas/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.