prm*_*lmu 6 python duplicates dataframe pandas
我有以下简单的数据框架
import pandas as pd
df = pd.DataFrame({'column_a': ['a', 'b', 'c', 'd', 'e'],
'column_b': ['b', 'x', 'y', 'c', 'z']})
column_a column_b
0 a b
1 b x
2 c y
3 d c
4 e z
Run Code Online (Sandbox Code Playgroud)
我想要显示两列中出现的字符串:
result = ("b", "c")
Run Code Online (Sandbox Code Playgroud)
谢谢
intersection这概括了任意数量的列.
set.intersection(*map(set, map(df.get, df)))
{'b', 'c'}
Run Code Online (Sandbox Code Playgroud)
使用python的set对象:
in_a = set(df.column_a)
in_b = set(df.column_b)
in_both = in_a.intersection(in_b)
Run Code Online (Sandbox Code Playgroud)