简体   繁体   中英

select rows from a DataFrame based on column values?

I want to filter a data frame based on a specific row value. I have a data frame consists of month and area of maize crop. I want to select the data frame during only for the month in which maize will be grown. I can assign it manually using isin but I have ahuge array and I wnat to automate. My goal is to filter the dataframe for a colmun called "Month" having a value betwen l and m.

When tried to input for the isin function a list it gives me only the first list element (i[0])?

for k in range (min(l,m), max(l,m)+1):
    n=np.array(k).reshape(1,)
    p=n.tolist()
    for i in range (len(p)):
        mask=df_area.loc[df_area["Month"].isin([p[i]])]

You can use a simple masking as @Gedas Miksenas suggested:

df_area[(df_area['month']>i) & (df_area['month']<m)]

or .query()

df_area.query('month > i an month < m')

or

df_area[df_area.month.between(left=i, right=m)]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM