0

I am new with panda. I've tried to look at the archive but could not find, or might be missing. the data:

d = {'A' : pd.Series([1., 2., 3., 4., 6.], index=['a', 'b', 'c', 'd','e']),'B' : pd.Series([3., 2., 1., 4., 7., 5.], index=['a', 'b', 'c', 'd', 'e', 'f',])}
df=pd.DataFrame(d)
df

what I want is trying to compare the value in 'A' vs 'B'. if value in 'B' is greater than that in 'A', I want to replace the value in 'B' with 'A' value.

the expected result in 'B' is becoming 1 (change), 2, 1, 4, 6 (change), 5

does anyone is willing to show me how to do it. many thanks,

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
giegie
  • 9
  • 1
  • Welcome to StackOverflow giegie! Please explain what you have tried so far and where it failed, StackOverflow is not a coding service. I suggest visiting [How to Ask](https://stackoverflow.com/help/how-to-ask) in order to get a better insight on how to ask a solid question. – nijm Oct 18 '18 at 09:34

1 Answers1

0

You can do this by calculating the row-wise minimum:

>>> df[['A', 'B']].min(axis=1)
a    1.0
b    2.0
c    1.0
d    4.0
e    6.0
f    5.0
dtype: float64 

So you can assign that Series back to the B column:

df['B'] = df[['A', 'B']].min(axis=1)

then the dataframe has as values:

>>> df
     A    B
a  1.0  1.0
b  2.0  2.0
c  3.0  1.0
d  4.0  4.0
e  6.0  6.0
f  NaN  5.0

In case B has a NaN value, then that one is "ignored" when calculating the minimum, and thus we will assign the value in the A column for that row.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555