0
votes

I have two csv files read into 2 dataframes:

df1:

 Aend   Bend

  A      B

  C      D

df2

 node_name Alarm

   A        alarm1

   D        alarm2

List =["NTP", "LOS"]

I want to check for row1 in df1 if columns Aend or Bend matches with node_name in df2. If yes take the corresponding row from df2 and have to check the condition

`if df2[Alarm] contains any value from List :

  print "YES"

else:

  print "NO"

Similarly check for row2 . What would be the best way to do this.

1
Thank you. I have updated my question. Please let me know if you need any further detailsPraveeda
List contains few strings that I want to check against. If these string is found in Alarm I want to add Yes into a new column or add No to that column.Praveeda

1 Answers

0
votes

There appear to be 2 issues you are trying to address:

  1. Filter df2 according to matching node_name in df1.
  2. Add a new column to df2 depending on whether Alarm is in an input list.

For the first part, you can construct a list of in-scope values and use pd.Series.isin:

# combine A_end and B_end values
vals = df1.values.ravel()

# filter df2
df2 = df2[df2['node_name'].isin(vals)]

For the second part, you can use numpy.where to create a new series based on a condition:

import numpy as np

L = ['NTP', 'LOS']

df2['NewCol'] = np.where(df2['Alarm'].isin(L), 'Yes', 'No')