-2

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
ayhan
  • 70,170
  • 20
  • 182
  • 203
  • There are only 2 rows of data? Please check [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) – jezrael Jul 02 '18 at 06: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 02 '18 at 07: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 02 '18 at 07: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 02 '18 at 07:14
  • Can you add 2, 3 new rows to sample data with expected output? – jezrael Jul 02 '18 at 07:26
  • yes. The idea is the difference in new row/save in new dataframe – throwaway073 Jul 02 '18 at 07:29

1 Answers1

0

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252