如何在python pandas中放置列公式?

时间:2018-07-26 08:36:08

标签: python-3.x pandas

输入:表1

Fruit Price_day_1 Price_Day_2 
Apple  45         43             
Orange 53         55
Mango  56         38 

预期输出:表2

 Fruit     Price_day
            _1      Price_Day_2   Difference=(Price_Day_1 - Price_Day_2)
    Apple  45         43             2
    Orange 53         55            -2
    Mango  56         38            18

如何使用熊猫创建公式并生成新列?

1 个答案:

答案 0 :(得分:1)

我认为需要减去列:

df['Difference=(Price_Day_1 - Price_Day_2)'] = df['Price_day_1'] - df['Price_Day_2']
print (df)
    Fruit  Price_day_1  Price_Day_2  Difference=(Price_Day_1 - Price_Day_2)
0   Apple           45           43                                       2
1  Orange           53           55                                      -2
2   Mango           56           38                                      18

每个公式都必须重写为熊猫代码。