pandas 数据框分配不会更新数据框 [英] pandas dataframe assign doesn't update the dataframe

查看:88
本文介绍了 pandas 数据框分配不会更新数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了虹膜数据集的熊猫数据框,我想在其中增加4列。列的内容必须为SepalRatio,PetalRatio,SepalMultiplied,PetalMultiplied。我使用了DataFrame的assign()函数添加了这四列,但是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.

我添加列的代码是:

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

在Jupyter笔记本中执行时,会显示正确的表,但如果我使用print语句,则不会添加四列。

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

Jupyter笔记本中的输出:

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

打印数据框后的输出:

      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  


推荐答案

您需要将输出分配给变量,例如:

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

使用更好仅一个 assign

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

这篇关于 pandas 数据框分配不会更新数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆