如何从python熊猫的两个系列中获取元素明智的交集

时间:2018-11-12 04:49:36

标签: intersection series elementwise-operations

我的问题是针对python熊猫。 我有两个系列,每个系列都有如下字符串元素: 为简化起见,我在DataFrame中串联了两个Series。

history: PropTypes.objectOf(PropTypes.func).isRequired,       // object of funcs
store: PropTypes.shape({                                      // shape of...
  dispatch: PropTypes.func.isRequired,                          // func
  getState: PropTypes.func.isRequired,                          // func
  liftedStore: PropTypes.objectOf(PropTypes.func).isRequired,   // object of funcs
  replaceReducer: PropTypes.func.isRequired,                    // func
  subscribe: PropTypes.func.isRequired,                         // func
  Symbol: PropTypes.func.isRequired,                            // func
}).isRequired

enter image description here

对此有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这是您可以做的:

import pandas as pd
import numpy as np

df1 = pd.DataFrame({'sr1' : ['ab','cd','ef'] ,
                    'sr2' : ['bz','ct','ka',]})

df1['intersection'] = df1.apply(lambda x: set(x.sr1) & set(x.sr2), axis=1)

df1['intersection'] = df1.intersection.apply(lambda x: list(x)[0] if len(x)>0 else np.nan)

输出:

enter image description here