3

I have a csv table with 3842 rows and 36columns similar to:

Time,chlo,coord1,coord2,coord3 

2003,0.52, NaN, NaN, 1.0

2003,0.56, NaN, 1.0, NaN

2003,0.58, 1.0, NaN, NaN

I need a code that will automatically replace 1.0 with the values from the left column CHLO, respectively, within each row. At the end, the column CHLO should disappear.

The final result would be similar to:

Time,coord1,coord2,coord3 

2003, NaN, NaN, 0.52

2003, NaN, 0.56, NaN

2003, 0.58, NaN, NaN

I am a beginner, I have learnt some basics of python and managed to write code for sorting the data to a certain level. But to do the above, I have no idea. I need this to organize data for a research project.

I read explanations about array, iterate, dict but I could not get to what I need. I would be extremely grateful if someone could give me a hint!

0

1 Answer 1

2

Use DataFrame.mask for replace by condition, DataFrame.pop is for extract column chlo.

If first column is not index:

df.iloc[:, 2:] = df.iloc[:, 2:].mask(df == 1, df.pop('chlo'), axis=0)
print (df)
   Time  coord1  coord2  coord3
0  2003     NaN     NaN    0.52
1  2003     NaN    0.56     NaN
2  2003     1.0     NaN     NaN

If first column is index:

df = df.mask(df == 1, df.pop('chlo'), axis=0)
print (df)

      coord1  coord2  coord3
Time                        
2003     NaN     NaN    0.52
2003     NaN    0.56     NaN
2003    0.58     NaN     NaN
Sign up to request clarification or add additional context in comments.

1 Comment

This was wonderful help! I solved the problem. Is there anything that I should do when the received answer solved my question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.