Home > Mobile >  How to access previous row to update the current row in python dataframe?
How to access previous row to update the current row in python dataframe?

Time:12-25

I am trying to calculate hospital census (number of patients in the hospital on a given day) using admissions and discharges. Census = census on previous day admissions - discharges.

Here is the code that I wrote:

# Calculate census
df['ICU census'] = 0
df['ICU census lag'] = df['ICU census'].shift(1)

df['ICU census'] = df['ICU census lag']   df['ICU admissions'] - df['ICU discharges']

The problem is that it's not dynamic in a sense that the lag of census is not updated when census gets changed. Is there a way to do this without a loop?

thank you

CodePudding user response:

If I understand you correctly, you would like that changing one column changes another column in a pandas DataFrame. If that's the case then you have to overload the brackets operator [] of a pandas DataFrame like so:

import pandas as pd


class MyDataFrame(pd.DataFrame):
    def __init__(self, data, columns=None):
        super().__init__(data=data, columns=columns)

    def __setitem__(self, key, value):
        if key == 'ICU census':
            ret = super().__setitem__(key, value)
            super().__setitem__('ICU census lag', self['ICU census'].shift(1))
            return ret
        else:
            return super().__setitem__(key, value)

Now you can initialize your DataFrame like so:

df = MyDataFrame(data=[[1,2,3],[4,5,6],[7,8,9]], columns=['ICU census','ICU census lag','ICU admissions'])

Print it:

print(df)
   ICU census  ICU census lag  ICU admissions
0           1               2               3
1           4               5               6
2           7               8               9

Modify ICU census and print:

df['ICU census']  = 1
print(df)
   ICU census  ICU census lag  ICU admissions
0           2             NaN               3
1           5             2.0               6
2           8             5.0               9

Notice that both the columns ICU census and ICU census lag have been modified.

CodePudding user response:

the main purpose of using pandas is for manipulating ,exploring and analyzing data , so it's not your best choice to track and update data , i recommend to use relational database (postgres) and then export your data "if you want" to csv file to analyze by pandas

CodePudding user response:

I don't really understand the question, but what I think you are asking is:

Can you go to a line number in Python?

The answer: unfortunately you cannot, but you can use a while loop.

  • Related