1

I have my table that looks like this:

column A column B
A
B
C
D
E
F
G

I want it to look like this:

column C
A
B
C
D
E
F
G
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135

3 Answers3

2

A column is major

df['column C'] = df['column A'].fillna(df['column B'])
smart lph
  • 131
  • 3
1

If you use pandas, you can bfill and get the first column:

df['column C'] = df[['column A', 'column B']].bfill(1).iloc[:,0]

output:

  column A column B column C
0        A      NaN        A
1        B      NaN        B
2        C      NaN        C
3      NaN        D        D
4      NaN        E        E
5        F      NaN        F
6      NaN        G        G
mozway
  • 194,879
  • 13
  • 39
  • 75
0

Not sure if that exactly what you want or its just a example but you could do something like this

This is coding from first principles

Column_A = ['A', "B",'C','','','F','']
Column_B = ['', "",'','D','E','','G']
Column_C = ['', "",'','','','','']

print('column C')

for i in range(len(Column_C)):
    if (Column_A[i] !='') :
        Column_C[i] = Column_A[i]
        print(Column_C[i])
    elif (Column_B[i] !='') :
        Column_C[i] = Column_B[i]
        print(Column_C[i])