简体   繁体   中英

Pandas create df that is the product of columns in another df

I have the following pandas dataframe:

id | a | b | c | d
------------------
 0 | 1 | 3 | 4 | 5
 1 | 2 | 3 | 5 | 6

and I would like to create a new df of which its columns is the difference of successive columns in the first df

new df:

id | b-a | c-b | d-c
--------------------
 0 |  2  |  1  |  1 
 1 |  1  |  2  |  1

Thank you

You can use sub with shift , for remove first column iloc :

df = df.set_index('id')
df = df.sub(df.shift(axis=1)).iloc[:, 1:].reset_index()
print (df)
   id    b    c    d
0   0  2.0  1.0  1.0
1   1  1.0  2.0  1.0

If need convert to int :

df = df.set_index('id')
df = df.sub(df.shift(axis=1)).iloc[:, 1:].astype(int).reset_index()
print (df)
   id  b  c  d
0   0  2  1  1
1   1  1  2  1

For changed column names:

df = df.set_index('id')
cols = df.columns

df = df.sub(df.shift(axis=1)).iloc[:, 1:].astype(int)
df.columns = cols[1:] + '-' + cols[:-1]
df = df.reset_index()
print (df)
   id  b-a  c-b  d-c
0   0    2    1    1
1   1    1    2    1

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