简体   繁体   中英

Selecting multiple rows from a dataframe using a column

So, I have a dataframe with a lot of rows and I'm trying to select just a column of some of those rows using a column of data. is there a command that asks for a column with certain values and returns the rows that contain that column? Oh, and I want to select just a column of the rows that are returned.

You are looking for df.loc

For example:

df.loc[df['col1']==3]

Returns all rows where col1 == 3

Or:

df.loc[df['col1']==3, ['col2', 'col1']]

Returns a dataframe like the one above but it only has col1 and col2

The documentation has a quick guide to this.

For selecting rows that meet a particular condition, you can use brackets.

df[(condtion)]
# ex.
df[df['A'] > 3]

That returns a subsetted dataframe, so you can select columns like you usually would.

df[df['A'] > 3][['A', 'B']]

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