Omer Elmasri
Omer Elmasri

Reputation: 45

How I can merge the columns into a single column in Python?

I want to merge 3 columns into a single column. I have tried changing the column types. However, I could not do it.

For example, I have 3 columns such as A: {1,2,4}, B:{3,4,4}, C:{1,1,1}

Output expected: ABC Column {131, 241, 441}

My inputs are like this:

df['ABC'] = df['A'].map(str) + df['B'].map(str) + df['C'].map(str)

df.head()

ABC {13.01.0 , 24.01.0, 44.01.0}

The type of ABC seems object and I could not change via str, int.

df['ABC'].apply(str)

Also, I realized that there are NaN values in A, B, C column. Is it possible to merge these even with NaN values?

Upvotes: 2

Views: 106

Answers (2)

MrCorote
MrCorote

Reputation: 563


# Example
import pandas as pd
import numpy as np

df = pd.DataFrame()

# Considering NaN's in the data-frame
df['colA'] = [1,2,4, np.NaN,5]
df['colB'] = [3,4,4,3,np.NaN]
df['colC'] = [1,1,1,4,1]

# Using pd.isna() to check for NaN values in the columns
df['colA'] = df['colA'].apply(lambda x: x if pd.isna(x) else str(int(x)))
df['colB'] = df['colB'].apply(lambda x: x if pd.isna(x) else str(int(x)))
df['colC'] = df['colC'].apply(lambda x: x if pd.isna(x) else str(int(x)))

# Filling the NaN values with a blank space
df = df.fillna('')

# Transform columns into string
df = df.astype(str)

# Concatenating all together
df['ABC'] = df.sum(axis=1)

Upvotes: 3

Mack123456
Mack123456

Reputation: 386

A workaround your NaN problem could look like this but now NaN will be 0

import numpy as np
df = pd.DataFrame({'A': [1,2,4, np.nan], 'B':[3,4,4,4], 'C':[1,np.nan,1, 3]})
df = df.replace(np.nan, 0, regex=True).astype(int).applymap(str)
df['ABC'] = df['A'] + df['B'] + df['C']

output

    A   B   C   ABC
0   1   3   1   131
1   2   4   0   240
2   4   4   1   441
3   0   4   3   043

Upvotes: 3

Related Questions