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
CodePudding user response:
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",
],
)