The best version of a question that relates to my question is found here. But I'm running into a hiccup somewhere.
My dataframe:
df = pd.DataFrame({'KEY': ['100000003', '100000009', '100000009', '100000009'],
'RO_1': [1, 1, 4,1],
'RO_2': [1, 0, 0,0],
'RO_3': [1, 1, 1,1],
'RO_4': [1, 4, 1,1]})
KEY RO_1 RO_2 RO_3 RO_4
0 100000003 1 1 1 1
1 100000009 1 0 1 4
2 100000009 4 0 1 1
3 100000009 1 0 1 1
I want to create 3 addition columns labeled 'Month1', 'Month2', to 'Month4'. Something simple like:
for i in range(3):
df.loc[1,'Month'+str(i)] = 1 # '1' is just there as a place holder
Although I'm getting a warning message when I execute this code:
"A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead"
I want to combine this with conditionals to fill in each cell for each column and each row.
The code below will create one one column and flag based on the condition if any column with RO_ has either condition
namelist = df.columns.get_values().tolist()
ROList = [s for s in namelist if "RO_" in s]
for col in ROList:
for i in range(3):
df['Month'] = np.where(np.logical_or(df[col]==4,df[col]==1), '1', '0')
df
I treid combining the two codes but I am missing a fundamental understanding of how to do this. Any help would be great.
Final expected result:
KEY RO_1 RO_2 RO_3 RO_4 Month1 Month2 Month3 Month4
0 100000003 1 1 1 1 1 1 1 1
1 100000009 1 0 1 4 1 0 1 1
2 100000009 4 0 1 1 1 0 1 1
3 100000009 1 0 1 1 1 0 1 1