Georg B
Georg B

Reputation: 311

Replacing all occurrences of a certain value with the respective value from another column in pandas

I have a large pandas DataFrame looking something like this :

   a  b   c   1    2    3    4  ...

1  1 200  A  T/T  C/C  ./.  A/A ...

2  1 258  C  ./.  C/C  T/T  A/A ...

3  1 400  G  G/G  C/G  ./.  ./. ...
...

I want to replace every instance of "./." in any of the numbered columns with the respective entry in column c.

What would be the best way to do so?

I know it could be solved with iterating over every row, but is there a "better", faster solution?

Upvotes: 0

Views: 224

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Just make use of apply() method:-

df=df.apply(lambda x:x.replace('./.',x['c']),axis=1)

Now if you print df you will get your desired output:

    a   b       c   1        2      3       4
1   1   200     A   T/T     C/C     A      A/A
2   1   258     C   C       C/C     T/T    A/A
3   1   400     G   G/G     C/G     G      G

Upvotes: 1

Related Questions