简体   繁体   中英

Select columns based on column name and location in Pandas

I have a dataframe with following columns for example: column names = ID, name, dob, away_dur, away_count, in_dur, in_time etc.

I would like to select data and create a new dataframe based on below conditions: 1. new dataframe should include columns between 4th to last and 2. new dataframe should not have columns starting with 'in_'

I know how to extract columns based on name or location. But can I know how to use both conditions to select data?

You can use

ndf = df.iloc[:,4:]
ndf = ndf.iloc[:,~ndf.columns.str.startswith('in_')]

Use a list comprehension to filter out columns. Then, use df.tail .

c = [x for x in df.columns if not x.startswith('in_')]
df = df[c].tail(-3)

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