当满足条件时,Pandas复制列中的特定单元格值

6tqwzwtp  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(190)

当A列满足以下特定条件时,我想复制B列中的单元格值,并创建一个新的C列:
1.“前进”切换到“停止”或“后退”时
1.“后退”切换到“停止”或“前进”时
例如,
| A类|B|C级|
| - ------|- ------|- ------|
| 停|无|无|
| 前进|十个|无|
| 前进|二十个|无|
| 前进|三十|三十|
| 向后|十个|无|
| 向后|二十五|无|
| 向后|四十|四十|
| 停|无|无|
| 停|无|无|
| 向后|五个|无|
| 向后|十个|十个|
| 前进|二十个|无|
| 前进|三十|三十|
| 停|无|无|
我试着按照代码,但它不工作,列C存储什么.

for idx, row in df.iterrows():
            if idx <= 1:
                continue

            A1 = row['A']
            A0 = df.iloc[idx + 1]['A']
            B1 = row['B']

            def C(df):
                if A1 == "Forward" and A0 == "Stop":
                    return B1
                if A1 == "Backward" and A0 == "Stop":
                    return B1
                elif A1 == "Forward" and A0 == "Backward":
                    return B1
                elif A1 == "Backward" and A0 == "Forward":
                    return B1

        df['C'] = df.apply(C, axis = 1)

我也试过这个,但是它不起作用

for idx, row in df.iterrows():
            if idx <= 1:
                continue

            A1 = df.iloc[idx - 1]['A']
            A0 = row['A']
            B1 = df.iloc[idx - 1]['B']

            def C(df):
                if A1 == "Forward" and A0 == "Stop":
                    return B1
                if A1 == "Backward" and A0 == "Stop":
                    return B1
                elif A1 == "Forward" and A0 == "Backward":
                    return B1
                elif A1 == "Backward" and A0 == "Forward":
                    return B1

        df['C'] = df.apply(C, axis = 1)
ioekq8ef

ioekq8ef1#

使用布尔掩码:

# is the value a Forward/Backward?
m1 = df['A'].isin(['Forward', 'Backward'])

# Is there a change of Forward/Backward?
s  = df['A'].where(m1)
m2 = s.ne(s.shift(-1))

# if both condition above are met assign 0
df['C'] = df['B'].where(m1&m2, 0)

备选方案:

# is the value a Forward/Backward?
m1 = df['A'].isin(['Forward', 'Backward'])

# Is there not a change of Forward/Backward?
s  = df['A'].where(m1)
m2 = s.eq(s.shift(-1))

# if both condition above are different (XOR), assign 0
df['C'] = df['B'].where(m1!=m2, 0)

输出:

A   B   C
0       Stop   0   0
1    Forward  10   0
2    Forward  20   0
3    Forward  30  30
4   Backward  10   0
5   Backward  25   0
6   Backward  40  40
7       Stop   0   0
8       Stop   0   0
9   Backward   5   0
10  Backward  10  10
11   Forward  20   0
12   Forward  30  30
13      Stop   0   0

中间体:

# approach 1
           A   B   C     m1     m2  m1&m2
0       Stop   0   0  False   True  False
1    Forward  10  10   True  False  False
2    Forward  20  20   True  False  False
3    Forward  30   0   True   True   True
4   Backward  10  10   True  False  False
5   Backward  25  25   True  False  False
6   Backward  40   0   True   True   True
7       Stop   0   0  False   True  False
8       Stop   0   0  False   True  False
9   Backward   5   5   True  False  False
10  Backward  10   0   True   True   True
11   Forward  20  20   True  False  False
12   Forward  30   0   True   True   True
13      Stop   0   0  False   True  False

# approach 2
           A   B   C     m1     m2  m1!=m2
0       Stop   0   0  False  False   False
1    Forward  10   0   True   True   False
2    Forward  20   0   True   True   False
3    Forward  30  30   True  False    True
4   Backward  10   0   True   True   False
5   Backward  25   0   True   True   False
6   Backward  40  40   True  False    True
7       Stop   0   0  False  False   False
8       Stop   0   0  False  False   False
9   Backward   5   0   True   True   False
10  Backward  10  10   True  False    True
11   Forward  20   0   True   True   False
12   Forward  30  30   True  False    True
13      Stop   0   0  False  False   False

相关问题