Determining column value change and which change takes place

I am analysing eye-tracking data. I have a df with a column ‘row’ which tells me which image is looked at. I have 9 images which belong to 3 categories.

Category 1 = [1,2,3]
Category 2 = [4,5,6]
Category 3 = [7,8,9]

I want a new column “change” which tell me whenever the number changes in the column “roi” and if the change is within or between categories.

If the change takes place between numbers from the list Category 1 than ‘C1’ should appear.

If the change takes place between numbers from the list Category 2 than ‘C2’ should appear.

If the change takes place between numbers from the list Category 3 than ‘C3’ should appear.

If the change takes place between numbers which are not in the same list than ‘X’
should appear.

Roi =[3,3,3,2,2,5,5,6,6,9,7,1]
Change = ['nan','nan','nan','C1','nan','X','nan','C2','nan','X','C3','X']
zipped = list(zip(Roi, Change))
df = pd.DataFrame(zipped, columns=['Roi', 'Change'])
print(df)

    Roi Change
0     3    nan
1     3    nan
2     3    nan
3     2     C1
4     2    nan
5     5      X
6     5    nan
7     6     C2
8     6    nan
9     9      X
10    7     C3
11    1      X

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use np.select:

C1 = [1,2,3]
C2 = [4,5,6]
C3 = [7,8,9]

roi = df["Roi"]
prev_roi = roi.shift()

df["Change"] = np.select(
    [
        (roi == prev_roi) | prev_roi.isnull(),
        roi.isin(C1) & prev_roi.isin(C1),
        roi.isin(C2) & prev_roi.isin(C2),
        roi.isin(C3) & prev_roi.isin(C3),
        roi != prev_roi
    ],
    [
        "nan",
        "C1",
        "C2",
        "C3",
        "X",
    ],
)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x