user5739619
user5739619

Reputation: 1838

Assign values in Pandas series based on condition?

I have a dataframe df like

A B
1 2
3 4

I then want to create 2 new series

t = pd.Series()
r = pd.Series()

I was able to assign values to t using the condition cond as below

t = "1+" + df.A.astype(str) + '+' + df.B.astype(str)
cond = df['A']<df['B']
team[cond] = "1+" + df.loc[cond,'B'].astype(str) + '+' + df.loc[cond,'A'].astype(str)

But I'm having problems with r. I just want r to contain values of 2 when con is satisfied and 1 otherwise

If I just try

r = 1
r[cond] = 2

Then I get TypeError: 'int' object does not support item assignment

I figure I could just run a for loop through df and check the cases in cond through each row of df, but I was wondering if Pandas offers a more efficient way instead?

Upvotes: 2

Views: 4006

Answers (2)

Alexander
Alexander

Reputation: 109546

You will laugh at how easy this is:

r = cond + 1

The reason is that cond is a boolean (True and False) which evaluate to 1 and 0. If you add one to it, it coerces the boolean to an int, which will mean True maps to 2 and False maps to one.

df = pd.DataFrame({'A': [1, 3, 4], 
                   'B': [2, 4, 3]})

cond = df['A'] < df['B']

>>> cond + 1
0    2
1    2
2    1
dtype: int64

Upvotes: 2

Stephen Ingram
Stephen Ingram

Reputation: 190

When you assign 1 to r as in

r = 1

r now references the integer 1. So when you call r[cond] you're treating an integer like a series.

You want to first create a series of ones for r the size of cond. Something like

r = pd.Series(np.ones(cond.shape))

Upvotes: 0

Related Questions