. Advertisement .
..3..
. Advertisement .
..4..
The need to conduct Pandas concatenate two columns must have arisen for many of us at least once. If that’s the case, we’re here to save you.
Learn more by reading on!
Dataframes Example
As a beginning, implementing the Pandas library and creating a fundamental DataFrame will be the first stage to fulfill this illustration.
# Create a pandas DataFrame.
import pandas as pd
englishteaching = ({
'Teachers':["Simmon","Pinmark","Hanson","Putin","Plex","Hanson","Simmon","Putin"],
'Fee' :[32000,45000,33000,44000,36000,45000,35000,42000],
'Duration':['50days','30days','40days','35days','35days','60days','50days','55days'],
'Discount':[2000,1300,2000,1100,2500,2300,1200,1500]
})
df = pd.DataFrame(englishteaching, columns=['Teachers','Fee','Prolongation','Discount'])
print(df)
In doing so, we arrive to the following outcome:
Teachers Fee Prolongation Discount
0 Simmon 32000 50days 2000
1 Pinmark 45000 30days 1300
2 Hanson 33000 40days 2000
3 Putin 44000 35days 1100
4 Plex 36000 35days 2500
5 Hanson 45000 60days 2300
6 Simmon 35000 50days 1200
7 Putin 42000 55days 1500
Pandas Concatenate Two Columns – How To
Using + Operator
In a Pandas DataFrame, concatenating two or more text/string columns is as simple as using the + operator.
Bear in mind that when you use the + operator on a column of numeric data, addition is performed rather than concatenation.
# Using + operator to combine two columns
df["Period"] = df['Teachers'].astype(str) +"-"+ df["Prolongation"]
print(df)
Teachers Fee Prolongation Discount Period
0 Simmon 32000 50days 2000 Simmon-50days
1 Pinmark 45000 30days 1300 Pinmark-30days
2 Hanson 33000 40days 2000 Hanson-40days
3 Putin 44000 35days 1100 Putin-35days
4 Plex 36000 35days 2500 Plex-35days
5 Hanson 45000 60days 2300 Hanson-60days
6 Simmon 35000 50days 1200 Simmon-50days
7 Putin 42000 55days 1500 Putin-55days
Using Series.str.cat() Function
It is possible to concatenate two Series by a delimiter or separator by using the series.str.cat() method. This may be used as seen below using a DataFrame. df[“courses”] & df[“Duration”] return a series in this case.
# Using Series.str.cat() function
df["Period"] = df["Teachers"].str.cat(df["Prolongation"], sep = "-")
print(df)
As a consequence, the output is the same as it was previously.
Using The Concat() Method
You may also use the DataFrame.agg() function to concatenate several string columns. As such, you should have combined all the columns into a list, just like how you did previously.
# Using DataFrame.agg() to combine two columns of text
df["period"] = df[['Teachers', 'Prolongation']].agg('-'.join, axis=1)
print(df)
As a result, an identical output as before is produced.
Using DataFrame.apply() and Lambda Function
To do the same thing, utilize the apply() function with a lambda. By substituting any column slice from your DataFrame for df[[“Courses”, “Duration”]], you may extend this approach to any number of string columns.
# Using DataFrame.apply() and lambda function
df["Period"] = df[["Teachers", "Prolongation"]].apply(lambda x: " ".join(x), axis =1)
print(df)
As a consequence, the output is the same as it was previously.
Conclusion
Above are a few options that can be of use when implementing Pandas concatenate two columns. Hopefully, this post will somehow lift up your coding. See then!
Leave a comment