简体   繁体   中英

Is there a way to group by logical comparison of two columns in Pandas?

I have aa dataframe with the following structure:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 1152 entries, 0 to 143
Data columns:
cuepos             1152  non-null values
response           1152  non-null values
soa                1152  non-null values
targetpos          1152  non-null values
testorientation    1152  non-null values
dtypes: float64(3), int64(2)

The cuepos column and the targetpos column both contain integer values of either 1 or 2 .

I would like to group this data by congruency between cuepos and targetpos . In other words, I would like to produce two groups, one for rows in which cuepos == targetpos and another group for which cuepos != targetpos .

I can't seem to figure out how I might do this. I looked at using grouping functions, but these seem only to act on a single column... or am I mistaken? Can someone point me in the right direction?

Thanks in advance! Blz

you can group by multiple columns:

df.groupby(['col1', 'col2']).apply(lambda x: x['col1'] == x['col2'], axis=1)

you can also use a mask:

df[df.col1==df.col2]

Note, if you goal is to do group computations, you can do

df.groupby(df.col1 == df.col2).apply(f)

and the result will be keyed by True/False.

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