Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace values from column series into respective row valued 1

Tags:

python

pandas

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!

like image 412
Andurush Avatar asked May 08 '26 10:05

Andurush


1 Answers

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
like image 126
jezrael Avatar answered May 10 '26 23:05

jezrael