zituo li
zituo li

Reputation: 21

i want to filter data with &,but got a wrong result

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

below is my code

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))

this is the data before processing

original data

this is the result i got


result

sationuuid great than 1 was also set to nan,if statement seems only do busSpeed<3,how this happend

Upvotes: 0

Views: 64

Answers (1)

Henry Woody
Henry Woody

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

Related Questions