Create columns, display row values ​​across multiple columns

I want to create a new column in mine df

, the values ​​of that column will be mapped to values ​​from several other columns.

I currently have this:

df['PLATFORM'] = df['ID_1'].map(lambda x: 'ID_1_MATCH' if x == 9 else 0)
df['PLATFORM'] = df['ID_2'].map(lambda x: 'ID_2_MATCH' if x == 10 else 0)
df['PLATFORM'] = df['ID_3'].map(lambda x: 'ID_3_MATCH' if x == 11 else 0)

      

Using this approach, the value in the new column will be overwritten in the second and third map (where the x

lambda expression criteria are met). I only want to update the column where 0 remains after the previous card.

Is there a way to map values ​​to a new column dependent on row values ​​in multiple other columns in a hierarchical manner?

+3


source to share


2 answers


I think you need numpy.where

:

df['PLATFORM'] = np.where(df['ID_1']  == 9, 'ID_1_MATCH', 
                 np.where(df['ID_2']  == 10, 'ID_2_MATCH', 
                 np.where(df['ID_3']  == 11, 'ID_3_MATCH', 0 )))

      



Example:

df = pd.DataFrame({'ID_1':[9,2,3,4],
                   'ID_2':[4,10,6,1],
                   'ID_3':[7,8,11,0]})

print (df)
   ID_1  ID_2  ID_3
0     9     4     7
1     2    10     8
2     3     6    11
3     4     1     0

df['PLATFORM'] = np.where(df['ID_1']  == 9, 'ID_1_MATCH', 
                 np.where(df['ID_2']  == 10, 'ID_2_MATCH', 
                 np.where(df['ID_3']  == 11, 'ID_3_MATCH', 0 )))
print (df)
   ID_1  ID_2  ID_3    PLATFORM
0     9     4     7  ID_1_MATCH
1     2    10     8  ID_2_MATCH
2     3     6    11  ID_3_MATCH
3     4     1     0           0

      

+3


source


Use a bit mask to set only the lines where your condition is true.

It's more idiomatic (and faster) than any maps or lambdas.



>>> df = pandas.DataFrame(columns=['x', 'y'], data=[[0,1], [1,2]])
>>> df
   x  y
0  0  1
1  1  2
>>> df.ix[df['x'] % 2 == 0, 'match'] = 'x is even'
>>> df.ix[df['y'] % 2 == 0, 'match'] = 'y is even'
>>> df
   x  y      match
0  0  1  x is even
1  1  2  y is even
>>> 

      

+1


source







All Articles