计算多列python中字符串的实例

2024-04-23 10:36:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下简单的数据帧

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

我要在两个列中显示的字符串:

^{pr2}$

谢谢


Tags: 数据字符串importdataframepandasdfascolumn
3条回答

类似于Sandeep Kadapa的解决方案。(没有tolistloc。)

>>> tuple(df['column_a'][df['column_a'].isin(df['column_b'])])                                            
('b', 'c')

intersection

这概括了任意数量的列。在

set.intersection(*map(set, map(df.get, df)))

{'b', 'c'}

使用python的set对象:

in_a = set(df.column_a)
in_b = set(df.column_b)
in_both = in_a.intersection(in_b)

相关问题 更多 >