Aug-26-2021, 08:17 PM
(This post was last modified: Aug-26-2021, 08:17 PM by eddywinch82.)
Hi JosepMaria,
Here is a method, to help you get a solution, I havn't got it quite right yet, but one way, is that you can use Numpy select with conditions.
I hope this helps you.
Could someone clean up the following attempt of mine, to give JosepMaria, the answer he needs ? If that is okay ? Why are the last two Rows values of the DataFrame, not changing to 9 and 0 respectively in the 'c1' Column, when the Code is run ?
Eddie Winch
Here is a method, to help you get a solution, I havn't got it quite right yet, but one way, is that you can use Numpy select with conditions.
I hope this helps you.
Could someone clean up the following attempt of mine, to give JosepMaria, the answer he needs ? If that is okay ? Why are the last two Rows values of the DataFrame, not changing to 9 and 0 respectively in the 'c1' Column, when the Code is run ?
import pandas as pd import numpy as np df = pd.DataFrame( {'user': {0: 1, 1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 2}, 'date': {0: '1995-09-01', 1: '1995-09-02', 2: '1995-10-03', 3: '1995-10-04', 4: '1995-10-05', 5: '1995-11-07', 6: '1995-11-08'}, 'x': {0: '1995-09-02', 1: '1995-09-02', 2: '1995-09-02', 3: '1995-10-05', 4: '1995-10-05', 5: '1995-10-05', 6: '1995-10-05'}, 'y': {0: '1995-10-03', 1: '1995-10-03', 2: '1995-10-03', 3: '1995-11-08', 4: '1995-11-08', 5: '1995-11-08', 6: '1995-11-08'}, 'c1': {0: '1', 1: '0', 2: '0', 3: '2', 4: '0', 5: '9', 6: '0'}, 'c2': {0: '1', 1: '0', 2: '0', 3: '2', 4: '0', 5: '9', 6: '0'}, 'c3': {0: '1', 1: '0', 2: '0', 3: '2', 4: '0', 5: '9', 6: '0'}, 'VTX1': {0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 6: 0}, 'VTY1': {0: 0, 1: 1, 2: 0, 3: 0, 4: 0, 5: 1, 6: 0}} ) df['date']= pd.to_datetime(df['date']).dt.strftime('%d-%b-%Y') df['x']= pd.to_datetime(df['x']).dt.strftime('%d-%b-%Y') df['y']= pd.to_datetime(df['y']).dt.strftime('%d-%b-%Y') df = df.astype(str) col = 'c1' conditions = [ df['date'].str.contains('Sep|Oct') == df['x'].str.contains('Sep'), df['date'].str.contains('Oct') == df['y'].str.contains('Nov'), df['date'].str.contains('07'), df['date'].str.contains('08')] #conditions = [ df['date'].eq(df['x']), (df['date'].eq(df['x']) & (df['user'].str.contains('2')))] choices = ['1', '2', '9', '0'] df['c1'] = np.select(conditions, choices) dfBest Regards
Eddie Winch