0

Im a beginner in Data Science and was wondering if I can make a column in a certain dataframe that always updates when a given cell has its values changed.

As an example:

In the famous Iris df, we have some numeric columns, such as: SepalLengthCm andSepalWidthCm.

    Id  SepalLengthCm   SepalWidthCm    PetalLengthCm   PetalWidthCm    Species
0   1   5.1 3.5 1.4 0.2 Iris-setosa 
1   2   90.0    3.0 1.4 0.2 Iris-setosa
2   3   4.7 3.2 1.3 0.2 Iris-setosa 
3   4   4.6 3.1 1.5 0.2 Iris-setosa 
4   5   5.0 3.6 1.4 0.2 Iris-setosa 
... ... ... ... ... ... ... ...
145 146 6.7 3.0 5.2 2.3 Iris-virginica  
146 147 6.3 2.5 5.0 1.9 Iris-virginica  
147 148 6.5 3.0 5.2 2.0 Iris-virginica  
148 149 6.2 3.4 5.4 2.3 Iris-virginica  
149 150 5.9 3.0 5.1 1.8 Iris-virginica  

If i, for some reason, wish to create a new column that sums these two columns, I can do:

df['Sum'] = df['SepalLengthCm']+df['SepalWidthCm']

But when a value in these two columns is changed, the sum doesn`t follow recalculating.

Is it possible to create a function that is aware of changes in columns?

  • 1
    There is no such feature. Depending on your situation you can create a function that will update the column when you call it or create a more complex class that wraps around the dataframe as suggested in this answer: https://stackoverflow.com/a/59413041/4145941 – Mohammad Jul 31 '21 at 15:49

0 Answers0