Reputation: 21
Here is my dataframe,i want to set the value satisfy busSpeed<3
and sationUuid==1
in the same time to be NAN ,but i got a wrong result ,anyone help, thanks
import pandas as pd
import numpy as np
df=pd.read_excel('d:gps/9-20-32-36574.xlsx')
df.sationUuid.fillna(method='bfill',inplace=True)
df.dropna(subset=['sationUuid'],inplace=True)
df1=list()
for i in range(len(df)):
if((df.sationUuid[i]==1)&(df.busSpeed[i]<3)):
df1.append(df.replace(df.busSpeed[i],np.NAN))
sationuuid
great than 1 was also set to nan,if
statement seems only do busSpeed<3
,how this happend
Upvotes: 0
Views: 64
Reputation: 15672
You can do this with Pandas querying rather than with a loop and if
block. Using Pandas querying has the added benefit of being much more efficient than using a loop. To replace the values satisfying your condition, you can use assignment with .loc
.
Here's an example:
import pandas as pd
import numpy as np
df = pd.read_excel('d:gps/9-20-32-36574.xlsx')
df.sationUuid.fillna(method='bfill',inplace=True)
df.dropna(subset=['stationUuid'],inplace=True)
mask = (df.stationUuid == 1) & (df.busSpeed < 3)
df.loc[mask, 'busSpeed'] = np.nan
So if df
looks like the following after reading in from excel:
stationUuid busSpeed
1 1.5
2 1
1 100
3 10
Then df
will look like this after the rest of the script:
stationUuid busSpeed
1 NaN
2 1
1 100
3 10
Upvotes: 1