将列系列中的值替换为各自的行值1

时间:2019-07-22 09:02:30

标签: python pandas

我有一个包含3842行和36列的csv表,类似于:

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

我需要一个代码,该代码将自动在每行中分别用左列CHLO中的值替换1.0。最后,CHLO列应消失。

最终结果将类似于:

Time,coord1,coord2,coord3 

2003, NaN, NaN, 0.52

2003, NaN, 0.56, NaN

2003, 0.58, NaN, NaN

我是一个初学者,我已经学习了python的一些基础知识,并设法编写了将数据排序到特定级别的代码。但是要做到以上,我不知道。我需要它来组织研究项目的数据。

我阅读了有关数组,迭代,字典的解释,但我无法找到所需的内容。如果有人可以给我提示,我将万分感谢!

1 个答案:

答案 0 :(得分:2)

使用DataFrame.mask替换条件,DataFrame.pop替换提取列chlo

如果第一列不是索引:

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

如果第一列是索引:

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