pandas dataframe assign doesn't update the dataframe

后端 未结 1 1889
轻奢々
轻奢々 2020-12-19 15:20

I made a pandas dataframe of the Iris dataset and I want to put 4 extra column in it. The content of the columns have to be SepalRatio, PetalRatio, SepalMultiplied, PetalMul

相关标签:
1条回答
  • 2020-12-19 16:00

    You need assign output to variable like:

    iris = iris.assign(SepalRatio = iris['SepalLengthCm'] / iris['SepalWidthCm']).assign(PetalRatio = iris['PetalLengthCm'] / iris['PetalWidthCm']).assign(SepalMultiplied = iris['SepalLengthCm'] * iris['SepalWidthCm']).assign(PetalMultiplied = iris['PetalLengthCm'] * iris['PetalWidthCm'])
    

    Beter is use only one assign:

    iris = iris.assign(SepalRatio = iris['SepalLengthCm'] / iris['SepalWidthCm'],
                       PetalRatio = iris['PetalLengthCm'] / iris['PetalWidthCm'],
                       SepalMultiplied = iris['SepalLengthCm'] * iris['SepalWidthCm'],
                       PetalMultiplied = iris['PetalLengthCm'] * iris['PetalWidthCm'])
    
    0 讨论(0)
提交回复
热议问题