'Add row to a dataframe python [duplicate]

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1