aabujamra
aabujamra

Reputation: 4636

Selecting rows according to int and string criteria

I have a dataframe that looks like this:

    Capital Social  Mark    Porte
0   12345           B 
1   0               A
2   0               A
3   12345631        A

If Capital Social == 0 and Mark == A I want to turn Porte into Big. So I'm running the following code:

df['Porte'].loc[(df['Capital Social'] == 0) & (df['Mark'] == 'A')]='Big'

However, when I run it, the result I get is just the same df:

    Capital Social  Mark    Porte
0   12345           B 
1   0               A
2   0               A
3   12345631        A

If I print(df['Capital Social'].dtypes) I get Int64 as result.

If I print(df['Mark'].dtypes) I get object as result. I tried to run df['Mark']=df['Mark'].astype(str) but the result keeps being an object. Is this the problem or what am I missing?

Upvotes: 0

Views: 69

Answers (2)

jezrael
jezrael

Reputation: 862591

You are really close:

df.loc[(df['Capital Social'] == 0) & (df['Mark'] == 'A'), 'Porte']='Big'

Upvotes: 3

cs95
cs95

Reputation: 402413

The correct form is

df.loc[(df['Capital Social'] == 0) & (df['Mark'] == 'A'), 'Porte'] = 'Big'

df['Porte'] returns a view/copy, and calling loc will modify the copy instead, leaving the original dataFrame untouched.

Upvotes: 3

Related Questions