Reputation:
I have two column in dataframe Column_1 and Column_2 if both contain same value then marked it as 1 as 0
I tried this but it return list of size 1 and inside list it contain series of size 416
splitsE = [(df.COLUMN_1.str.contains(' Each')) &
(df.COLUMN_2.str.contains(' EACH'))]
i even tried this one also but it doesnt map
splitsE = [(df.COLUMN_1.str.contains(' Each')) &
(df.COLUMN_2.str.contains(' EACH'))]
splitsE = list(map(list, zip(*splitsE)))
df['CASE#'] = pd.Series(splitsE).map(({True:'1', False:'0'}))
if i want to update df['CASE#'] where it contain / as 2 in place of 0
COLUMN_3 CASE#
25/PACK 0
EACH 1
100/BOTTLE 0
25/PACK 0
NaN 0
3/PACK 0
EACH 1
Upvotes: 1
Views: 729
Reputation: 1469
If I correctly understand your question, here is the solution:
import pandas as pd
data = [[1,2], [2,5], [3,3], [4,7],[9,9], [6,5]]
df = pd.DataFrame(columns=['col1', 'col2'], data=data)
df.head()
df['comp'] = 1 * (df['col1'] == df['col2'])
df.head()
Upvotes: 0
Reputation: 862511
Use:
splitsE = (df.COLUMN_1.str.contains(' Each')) & (df.COLUMN_2.str.contains(' EACH'))
I think simpliest is convert boolean mask to integer for True/False
to 1/0
map:
df['CASE#'] = splitsE.astype(int)
Another solution is use numpy.where
:
df['CASE#'] = np.where(splitsE, 1, 0)
Or map
with dictionary with removed one ()
:
df['CASE#'] = splitsE.map({True:'1', False:'0'})
EDIT: For another condition use numpy.select
:
mask1 = df.COLUMN_3.str.contains('EACH', case=False, na=False)
mask2 = df.COLUMN_3.str.contains('/', case=False, na=False)
df['CASE#'] = np.select([mask1, mask2], [1, 2], default=0)
print (df)
COLUMN_3 CASE#
0 25/PACK 2
1 EACH 1
2 100/BOTTLE 2
3 25/PACK 2
4 NaN 0
5 3/PACK 2
6 EACH 1
Upvotes: 2