Sorry if the title is somehow long and weird...
I have the following csv file:
Date,Value1,Value2
01-01-01,01,01
02-01-01,02,00
03-01-01,03,01
04-01-01,04,01
In this dataset I would like to replace the data of Value1
column by a NaN
when the data of Value2
column equal 00
.
So the output dataset should look like this:
Date,Value1,Value2
01-01-01,01,01
02-01-01,NaN,00
03-01-01,03,01
04-01-01,04,01
I tried with the following codes but I am missing some of the conditions and it obviously does not work:
data = pd.read_csv("test.csv")
data['Value1'][data.Value2 == 00] = np.NaN
Does anyone knows how to achieve that?
import pandas as pd
import numpy as np
df=pd.read_csv('C:/Users/your_csv.csv')
df.loc[df['Value2'] == 0,'Value1']=np.nan
Output
Date Value1 Value2
0 1/1/2001 1.0 1
1 2/1/2001 NaN 0
2 3/1/2001 3.0 1
3 4/1/2001 4.0 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.