0

I have a dataframe that contains 4 columns as follows:

nb,state,freebk,freebs
901,1,6,14
903,0,2,18
904,1,10,20
905,1,15,5

I want to add all the rows that have state equal to 1 to a new dataframe

Desired new dataframe:

nb,state,freebk,freebs
    901,1,6,14
    904,1,10,20
    905,1,15,5

I tried the following

 openStationDF = pd.DataFrame(columns=['nb','state','freebk','freebs'])
    t = 0
    for i in range(len(stationDF)):
        station = stationDF.iloc[i]
        if station[0] == 1:
            openStationDF.loc[t] = station
            t = t + 1

This code added successfully the stations with state = 1 but what is happing is that the nb of the station is = NaN and the Name of the station started from 0 and then incrementing by one.

nb         NaN
state      1.0
freebk     6.0
freebs    14.0
Name: 0, dtype: float64
nb         NaN
state      1.0
freebk    10.0
freebs    20.0
Name: 1, dtype: float64
nb         NaN
state      1.0
freebk    15.0
freebs     5.0
Name: 2, dtype: float64

I want the name to equal the nb of the station(name = 901...)

Thanks...

0

1 Answer 1

2

Use query:

df_new = df.query('state == 1')

Or, use boolean indexing:

df_new = df[df['state'] == 1]

Output:

    nb  state  freebk  freebs
0  901      1       6      14
2  904      1      10      20
3  905      1      15       5
Sign up to request clarification or add additional context in comments.

2 Comments

would you have a pointer to the pros and cons of querying vs boolean indexing?
I like query for readability, but boolean indexing can be faster depending on data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.