我创建了一个示例数据框,如下所示:
A B A+B
0 1 2 3
1 9 60 69
2 20 400 420
我想显示这样的过程:是的,像my last question这样的过程,但是这次没有滚动窗口内容
A B A+B Equation
0 1 2 3 1+2
1 9 60 69 9+60 #the expectation
2 20 400 420 20+400
假设A列和B列是从这样的分离列中创建的:
d={'A':[1,9,20],'B':[2,60,400]}
这是我尝试过的一些代码:
df['A+B']=df['A']+df['B']
df['Process']=str(df['A'])+str(df['B'])
这是输出:
A B
0 1 2
1 9 60
2 20 400
A B A+B Process
0 1 2 3 0 1\n1 9\n2 20\nName: A, dtype: int...
1 9 60 69 0 1\n1 9\n2 20\nName: A, dtype: int...
2 20 400 420 0 1\n1 9\n2 20\nName: A, dtype: int... #Is there any step that i missed?
>>>
答案 0 :(得分:1)
根据Henry的建议,实现所需目标的最佳方法是:
df['Process'] = df['A'].astype(str) + '+' + df['B'].astype(str)
df
A B A+B Process
0 1 2 3 1+2
1 9 60 69 9+60
2 20 400 420 20+400
答案 1 :(得分:0)
您可以使用Apply功能
df['Process']= df.apply(lambda row : f"{row['A']}+{row['B']}", axis=1)
对我有用。