Home > Back-end >  How to merge two pandas columns into one column?
How to merge two pandas columns into one column?

Time:02-10

I merged two data frames together and I want to combine two pandas columns as follows:

df1:

A   B   C
1   3  NaN
2  Nan  2
3   5  NaN
4  NaN  1

I want to get a result like the following:

df1:

A   BC
1   3
2   2
3   5
4   1

CodePudding user response:

import pandas as pd

df = pd.read_csv("df1.csv")
df.fillna(0, inplace=True)
df["BC"] = df["B"]   df["C"]
df.drop(["B", "C"], axis=1, inplace=True)

CodePudding user response:

This is a simple way to do it by converting NaN to 1 and 'B' times 'C', as follows:

import pandas as pd
import numpy as np
df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': [3, np.nan, 5, np.nan],
    'C': [np.nan, 2, np.nan, 1]
})

print(df)
#   A    B    C
#0  1  3.0  NaN
#1  2  NaN  2.0
#2  3  5.0  NaN
#3  4  NaN  1.0

df['B'] = df['B'].fillna(1)
df['C'] = df['C'].fillna(1)
df['BC'] = df['B']*df['C']

df = df[['A', 'BC']]
print(df)
#   A   BC
#0  1  3.0
#1  2  2.0
#2  3  5.0
#3  4  1.0

CodePudding user response:

You could use bfill and ffill on your B,C columns, and concat the result with column A:

pd.concat([df['A'],
           df[['B','C']].bfill(axis=1).ffill(axis=1).iloc[:,-1].to_frame().rename({'C':'BC'},axis=1)],axis=1)

prints:

   A   BC
0  1    3
1  2  2.0
2  3    5
3  4  1.0
  • Related