There is a table:

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo", ... "bar", "bar", "bar", "bar"], ... "B": ["one", "one", "one", "two", "two", ... "one", "one", "two", "two"], ... "C": ["2017", "2017", "2018", "2017", ... "2018", "2017", "2018", "2017", ... "2017"], ... "D": [1, 2, 2, 3, 3, 4, 5, 6, 7]}) 
  1. It is necessary to put down the values ​​'Yes' in the column 'E' if the row A contains 'f' and the column 'B' has a value equal to 'one'

  2. It is necessary to put down in the 'D' column the value 'No' if the execution of a certain function returns True.

  3. It is necessary to put in the column 'F' values ​​by the condition of the dictionary, if the dictionary key is included in the string, then return the value by key.

  • First, there is no difficult condition. Secondly, what is the actual problem? - Enikeyschik
  • df ['E'] = np.where (df ['A']. str.contains ('fo'), 'Yes', 'No') - I was offered this construction in the previous question under one condition. But now there are several conditions and I cannot apply this construction. - Gremlin
  • Please give an example of what you want to get at the output - MaxU
  • I want to get a filled column in the date frame in accordance with the conditions - Gremlin 1:51 pm

1 answer 1

 # 1 df.loc[df['A'].str.contains('f') & (df['B'] == 'one'), 'E'] = 'Yes' # 2 def func(ser): return ser % 2 == 0 df.loc[func(df['D']), 'D'] = 'No' 

The third point without an example of a dictionary and what should come out is incomprehensible ...

Result:

 In [251]: df Out[251]: ABCDE 0 foo one 2017 1 Yes 1 foo one 2017 No Yes 2 foo one 2018 No Yes 3 foo two 2017 3 NaN 4 foo two 2018 3 NaN 5 bar one 2017 No NaN 6 bar one 2018 5 NaN 7 bar two 2017 No NaN 8 bar two 2017 7 NaN