Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

python - pandas dataframe assign doesn't update the dataframe

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, PetalMultiplied. I used the assign() function of the DataFrame to add this four columns but the DataFrame remains the samen.

My code to add column is :

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'])`

When executing in Jupyter notebook a correct table is shown but if I use the print statement the four column aren't added.

Output in Jupyter notebook :

    Id  SepalLengthCm   SepalWidthCm    PetalLengthCm   PetalWidthCm    Species SepalRatio  PetalRatio  SepalMultiplied PetalMultiplied
0   1   5.1 3.5 1.4 0.2 Iris-setosa 1.457143    7.000000    17.85   0.28
1   2   4.9 3.0 1.4 0.2 Iris-setosa 1.633333    7.000000    14.70   0.28
2   3   4.7 3.2 1.3 0.2 Iris-setosa 1.468750    6.500000    15.04   0.26
3   4   4.6 3.1 1.5 0.2 Iris-setosa 1.483871    7.500000    14.26   0.30
4   5   5.0 3.6 1.4 0.2 Iris-setosa 1.388889    7.000000    18.00   0.28
5   6   5.4 3.9 1.7 0.4 Iris-setosa 1.384615    4.250000    21.06   0.68
6   7   4.6 3.4 1.4 0.3 Iris-setosa 1.352941    4.666667    15.64   0.42
7   8   5.0 3.4 1.5 0.2 Iris-setosa 1.470588    7.500000    17.00   0.30
8   9   4.4 2.9 1.4 0.2 Iris-setosa 1.517241    7.000000    12.76   0.28
9   10  4.9 3.1 1.5 0.1 Iris-setosa 1.580645    15.000000   15.19   0.15

output after printing the dataframe :

      Id  SepalLengthCm  SepalWidthCm  PetalLengthCm  PetalWidthCm  
0      1            5.1           3.5            1.4           0.2   
1      2            4.9           3.0            1.4           0.2   
2      3            4.7           3.2            1.3           0.2   
3      4            4.6           3.1            1.5           0.2   
4      5            5.0           3.6            1.4           0.2   
5      6            5.4           3.9            1.7           0.4   
6      7            4.6           3.4            1.4           0.3   
7      8            5.0           3.4            1.5           0.2   
8      9            4.4           2.9            1.4           0.2   
9     10            4.9           3.1            1.5           0.1 

            Species  
0       Iris-setosa  
1       Iris-setosa  
2       Iris-setosa  
3       Iris-setosa  
4       Iris-setosa  
5       Iris-setosa  
6       Iris-setosa  
7       Iris-setosa  
8       Iris-setosa  
9       Iris-setosa  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...