I want to check all rows in dataframe,
if this row's
column1
== previous row'scolumn2
and
if this row's
column2
== previous row'scolumn1
then drop this row.
For example:
Column1 Column2
A B
B A
A C
A D
D A
B C
The result I want would be:
Column1 Column2
A B
A C
A D
B C
Please help, thank you!
CodePudding user response:
You can use
m1 = df['Column1'] == df['Column2'].shift()
m2 = df['Column2'] == df['Column1'].shift()
out = df[~(m1 & m2)]
print(out)
Column1 Column2
0 A B
2 A C
3 A D
5 B C
CodePudding user response:
You could do a shift on those columns then use conditional statement to check if the column values are equal.
import pandas as pd
data = [
['A', 'B'],
['B', 'A'],
['A', 'C'],
['A', 'D'],
['D', 'A'],
['B', 'C']]
cols = ['Column1', 'Column2']
df = pd.DataFrame(data, columns=cols)
df['Pre_Column2'] = df['Column2'].shift(1)
df['Pre_Column1'] = df['Column1'].shift(1)
df = df[~((df['Column1'] == df['Pre_Column2']) & (df['Column2'] == df['Pre_Column1']))]
df = df[cols]
Output:
print(df)
Column1 Column2
0 A B
2 A C
3 A D
5 B C