如何将一个DataFrame的列值添加到另一个DataFrame的另一列中

huangapple go评论91阅读模式
英文:

How to add a value of a column from one Dataframe to another column of another dataframe

问题

1st dataframe: 输出

A B C 
M 2 H

2nd Dataframe: 活动详情

P Q R
2 3 O

一个数据框中的 A、B、C 列与第二个数据框中的列相同,但名称不同,我想在第二个数据框中添加另一行

**输出

P Q R
2 3 O
M 2 H

请假设 A = P、B = Q 和 C = R

我正在尝试这个方法,但不起作用

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

使用这个方法,P 列中的所有行都被替换为:

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

还有其他方法可以做到吗?

英文:

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???

答案1

得分: 1

使用 concat 与通过 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
英文:

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

答案2

得分: 1

根据您的所需映射(A->P,B->Q,C->R),您可以使用rename,然后使用concat

# 名称的映射
d = {'A': 'P', 'B': 'Q', 'C': 'R'}

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

注:如果您想按列的顺序合并,您也可以定义 d = dict(zip(Output.columns, Campaign_Details.columns))

输出:

   P  Q  R
0  2  3  O
1  M  2  H
英文:

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

答案3

得分: 1

其他回答是合并DataFrame的正确方法。您必须使用pd.concat(请查看下面的原因),但它们没有解释您的错误。

append是一个方法而不是属性,所以您必须使用(...)来调用该方法。然而,这还不够。您想要附加什么?这就像Python中的list

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

# 输出
[0, 1, 2, 3]

与使用apply逐列添加不同,您可以直接将您的Output DataFrame附加到Campaign_Details。为此,您必须重命名Output DataFrame的列,以使它们的列名与Campaign_Details对齐。

@mozway使用映射字典确保A=P,B=Q和C=R。另一方面,@jezrael认为A=P,因为A和P是第一列,依此类推。

使用append

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

# 输出
   P  Q  R
0  2  3  O
0  M  2  H

也许Pandas会引发FutureWarning。如果您使用的是Pandas 2之前的版本,您可以使用append,但这个方法已从Pandas 2中删除,所以正确的方法是使用pd.concat

英文:

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.

huangapple
  • 本文由 发表于 2023年6月15日 15:22:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定