0

1st dataframe: Output

A B C 
M  2 H

2nd Dataframe: Campaign details

P Q R
2 3 O

A, B, C in one dataframe is same as the column in the 2nd dataframe, but with different name, I want to add another row to the 2nd dataframe

**Output

P Q R
2 3 O
M 2 H

Please assume A = P, B = Q and C = R

I am trying this but not working

output["A"] = Campaign_Details["P"].apply(lambda x: Campaign_Details.get(x)).append

With this , all the rows in P column are replaced by:

<bound method Series.append of Unnamed: 1 N...

Is there any other way to do this???

mozway
  • 194,879
  • 13
  • 39
  • 75

3 Answers3

1

Use concat with set same columns names by DataFrame.set_axis:

out = pd.concat([Campaign_Details, output.set_axis(Campaign_Details.columns, axis=1)],
                ignore_index=True)
print (out)
   P  Q  R
0  2  3  O
1  M  2  H
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

You can rename based on your desired mapping (A->P, B->Q, C->R), then concat:

# mapping for the names
d = {'A': 'P', 'B': 'Q', 'C': 'R'}

out = pd.concat([Campaign_Details, Output.rename(columns=d)],
                ignore_index=True)

NB. if you want to merge in order of the columns, you could also define d = dict(zip(Output.columns, Campaign_Details.columns)).

Output:

   P  Q  R
0  2  3  O
1  M  2  H
mozway
  • 194,879
  • 13
  • 39
  • 75
1

Other answers are the right way to merge your dataframe. You have to use pd.concat (see below why) but they don't explain your error.

append is a method not an attribute, so you have to call the method with (...). However it's not sufficient. What do you want to append? It's like list in python:

lst = [0, 1, 2]
lst.append(3)
print(lst)

# Output
[0, 1, 2, 3]

Instead of adding column by column with apply, you can directly append your Output dataframe to Campaign_Details. To do it, you have to rename the column of Output dataframe to align their column names with Campaign_Details

@mozway uses a mapping dict to be sure A=P, B=Q and C=R. On the other side, @jezrael considers A=P because A and P are the first columns and so on.

With append:

out = Campaign_Details.append(Output.set_axis(Campaign_Details.columns, axis=1))
print(out)

# Output
   P  Q  R
0  2  3  O
0  M  2  H

Maybe Pandas will raise a FutureWarning. If you use a version of Pandas prior 2, you can use append but this method has been removed from Pandas 2 so the right way is to use pd.concat.

Corralien
  • 109,409
  • 8
  • 28
  • 52