熊猫如何找到包含子串的单元格位置

时间:2018-04-16 07:08:31

标签: pandas numpy

示例:

Price   | Rate p/lot |  Total Comm|
 947.2      1.25        BAM 1.25

 129.3      2.1         $ 1.25

 161.69     $ 0.8         CAD 2.00

如果我搜索[' $',' CAD']: -

预期产出: -

[(1, 2), (2, 1),(2,2)]

3 个答案:

答案 0 :(得分:1)

您可以将inapplymap

一起使用
i, j = (df.applymap(lambda x: '$' in str(x))).values.nonzero()
t = list(zip(i, j))
print (t)
[(1, 2), (2, 1)]
i, j = (df.applymap(lambda x: any(y for y in L if y in str(x)))).values.nonzero()
#another solution
#i, j = (df.applymap(lambda x: any(s in str(x) for s in L))).values.nonzero()
t = list(zip(i, j))
print (t)

[(1, 2), (2, 1), (2, 2)]

答案 1 :(得分:1)

抱歉,找到这样的解决方案,这可能对某人有帮助

import pandas as pd

df = pd.DataFrame([[947.2, 1.25, 'BAM 1.25'],
                   [129.3, 2.1, '$ 1.25'],
                   [161.69, '0.8 $', 'CAD 2.00']],
                  columns=['Price', 'Rate p/lot', 'Total Comm'])


row, column = (df.applymap(lambda x: x if any(s in str(x) for s in ['$','CAD']) else None )).values.nonzero()
t = list(zip(row,column))

答案 2 :(得分:1)

使用str.contains

df = df.astype(str)

from itertools import product
result = reduce(lambda x,y:x+y, [list(product([i],list(df.iloc[:,i][df.iloc[:,i].str.contains('\$|CAD')].index))) for i in range(len(df.columns))])

输出

[(1, 2), (2, 1), (2, 2)]