Change values in each cell based on a comparison with another cell using pandas

Pythonidae :

I want to compare two values in column 0 to the values in all the other columns and change to values of those columns appropriately. I have 4329 rows x 197 columns.

From this:

  0  1  2  3
0 G  G  G  T
1 A  A  G  A
2 C  C  C  C
3 T  A  T  G

To this:

  0  1  2  3
0 G  1  1  0
1 A  1  0  1
2 C  1  1  1
3 T  0  1  0

I've tried a nested for loop, which does not work and is slow.

for index, row in df.iterrows():        
    for name, value in row.iteritems():
        if name == 0:
            c = value
            continue
        if value == c:
            value = 1
        else:
            value = 0

I haven't been able to piece together a way to use apply or applymap for the problem.

Quang Hoang :

Here's an approach with iloc and eq:

df.iloc[:,1:] = df.iloc[:,1:].eq(df.iloc[:,0], axis=0).astype(int)

Output:

   0  1  2  3
0  G  1  1  0
1  A  1  0  1
2  C  1  1  1
3  T  0  1  0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26572&siteId=1