简体   繁体   中英

How to substract rows using python Pandas?

I have a dataframe which looks like this:

        C/A  UNIT       SCP     DATEn     TIMEn    DESCn  ENTRIESn    EXITSn  
0     A002  R051  02-00-00  05-01-11  00:00:00  REGULAR   3144312   1088151 
1     A002  R051  02-00-00  05-01-11  04:00:00  REGULAR   3144335   1088159 
2     A002  R051  02-00-00  05-01-11  08:00:00  REGULAR   3144353   1088177 
3     A002  R051  02-00-00  05-01-11  12:00:00  REGULAR   3144424   1088231   
4     A002  R051  02-00-00  05-01-11  16:00:00  REGULAR   3144594   1088275  
5     A002  R051  02-00-00  05-01-11  20:00:00  REGULAR   3144808   1088317   
6     A002  R051  02-00-00  05-02-11  00:00:00  REGULAR   3144895   1088328

I need to create a new row (ENTRIESn_hourly) which would be the resulting from subtracting each row in the column ENTRIESn from the previous row. Could anyone help? Cheers!

Dani

Just call diff :

In [26]:

df['Entries diff']= df['ENTRIESn'].diff()
df
Out[26]:
        C/A  UNIT       SCP     DATEn     TIMEn    DESCn  ENTRIESn   EXITSn  \
index                                                                         
0      A002  R051  02-00-00  05-01-11  00:00:00  REGULAR   3144312  1088151   
1      A002  R051  02-00-00  05-01-11  04:00:00  REGULAR   3144335  1088159   
2      A002  R051  02-00-00  05-01-11  08:00:00  REGULAR   3144353  1088177   
3      A002  R051  02-00-00  05-01-11  12:00:00  REGULAR   3144424  1088231   
4      A002  R051  02-00-00  05-01-11  16:00:00  REGULAR   3144594  1088275   
5      A002  R051  02-00-00  05-01-11  20:00:00  REGULAR   3144808  1088317   
6      A002  R051  02-00-00  05-02-11  00:00:00  REGULAR   3144895  1088328   

       Entries diff  
index                
0               NaN  
1                23  
2                18  
3                71  
4               170  
5               214  
6                87 

You can also do that by first shifting the rows by one:

df["ENTRIESn_hourly"] = df["ENTRIESn"] - df["ENTRIESn"].shift()

See pandas.DataFrame.shift .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM