2

I need to set a variable in Column B based on value in Column A as in :

ABC 1
PQR -
ABC -
PQR -
ABC -
PQR -

In my data, against some values of Column A I have a '1' set in column B. But the issue is it is against only the first occurrence. Like for 'ABC' above, '1' is set only for the first occurrence.

I need to set the '1' for all such remaining values. PS - There are a lot of entries so I can't hard-code values like 'For all ABC set column B to 1'

I've tried the following logic -

#Filter out entries with a '1' set
df_one = df_Consolidated[df_Consolidated['Val'] == 1]

#Store these values in a list
list_l2 = []
for s in df_one:
    list_l2.append(df_one['Text String'])

#Check in the dataframe column once again iterating over the list

But I don't think this is the best or the correct approach to do this. I'm looking for a simple yet effective solution.

  • I think simpliest is `df.loc[df["A"] == "ABC", "B"] = 1` – jezrael Mar 25 '19 at 06:52
  • @jezrael he said only first occurrence of the Text has value 1, so using "ABC" is making that static. which won't be able to solve for other occurrence of Text like "ABC" – Nihal Mar 25 '19 at 07:20
  • 1
    OK, so change `df.loc[df['Text'].isin((df.loc[df['val'] == '1', 'Text'])), 'val'] = '1'` – jezrael Mar 25 '19 at 07:24

1 Answers1

2

data(test.csv):

Text,val
ABC,1
PQR,-
ABC,-
PQR,-
ABC,-
PQR,-

code:

df = pd.read_csv('test.csv')
df.loc[df['Text'].isin((df[df['val'] == '1']['Text'])), 'val'] = '1'
print(df)

output:

  Text val
0  ABC   1
1  PQR   -
2  ABC   1
3  PQR   -
4  ABC   1
5  PQR   -

explanation:

here df[df['val'] == '1']['Text'] will get all Text has val = '1'

df['Text'].isin((df[df['val'] == '1']['Text']) will check for each row has Text in df[df['val'] == '1']['Text'] it will return Boolean

like

0     True
1    False
2     True
3    False
4     True
5    False

and df.loc to assign val to 1

Nihal
  • 5,262
  • 7
  • 23
  • 41