Difference between rows in pandas

Multi tool use
Multi tool use


Difference between rows in pandas



I have data in csv which i am reading with pandas. The data is of this format-


name company income saving
A AA 100 10

B AA 200 20



I wish to create a new row with name A, company AA and income and saving being difference of A and B.



Expected output-


name company income saving
A AA -100 -10





There are only 2 rows of data? Please check how to provide a great pandas example as well as how to provide a minimal, complete, and verifiable example
– jezrael
Jul 2 at 6:56





I have updated by question. There are lots of rows. I shall be able to do that if i can figure out this minimal example
– throwaway073
Jul 2 at 7:05





So what should contain the result from the third row? What should happen if company differs? What was your approach and why did it fail?
– Mr. T
Jul 2 at 7:08






The companies won't differ. I shall take care by filtering. Basically i can't figure out when i take difference, I don't want the first 2 columns to subtract as they are string. I only want the columns having numerical value to subtract. (all columns after 2nd column are numerals)
– throwaway073
Jul 2 at 7:14





Can you add 2, 3 new rows to sample data with expected output?
– jezrael
Jul 2 at 7:26




1 Answer
1



I believe need:


print (df)
name company income saving
0 A AA 100 10
1 B AA 200 20
2 C AA 300 40

#for select columns by names
df1 = df[['name','company']].join(df[['income','saving']].diff(-1))
#for select columns by positions
#df1 = df.iloc[:, :2].join(df.iloc[:, 2:].diff(-1))

print (df1)
name company income saving
0 A AA -100.0 -10.0
1 B AA -100.0 -20.0
2 C AA NaN NaN






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

i wZLBwHG6h 9jsC2,DHNKrpfbEf9jUWi2 ns5RMIL,6,5P9N8P,Im
qZMsq4kNdw,BFwP3 p5kKtv,AOfT9JnSHW5UM9t K WGZ9J NI9WG,R3ZXSyrcQBkq,o7,bXZ16Xuu svZMVrR92sC8,4MMflOlJ

Popular posts from this blog

Rothschild family

Boo (programming language)