2

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?

5
  • 1
    You should see this post. It answers basically the same question. Commented Aug 9, 2019 at 0:20
  • 1
    @Pedreo: No it doesnt... because the frame has NaNs Commented Aug 9, 2019 at 0:24
  • 1
    @Omer how do you want the NaNs to be included in the final output? and please update your question including NaN in your example table. Otherwise you won't get a correct answer Commented Aug 9, 2019 at 0:24
  • @Mack123456 True! I'll edit my answer. Commented Aug 9, 2019 at 0:30
  • Well technically, based on the current example df given, your answer is correct. Commented Aug 9, 2019 at 0:33

2 Answers 2

3

# 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)

Sign up to request clarification or add additional context in comments.

2 Comments

The problem in the dataframe is the NaN. I have no idea how your solution solves this. Because of the NaN the numbers are floats > 1.0 3.0 etc.. enhance OPs question.
Indeed. Depending on what OP wants you can use: df['ABC'] = df.sum(axis=1).apply(int).astype(str). that would give a str as output.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.