2024-05-01 21:46:04 发布
网友
我有下面的数据框,我想找到的索引单元格是从某些字符串开始。在
示例:
Price | Rate p/lot | Total Comm| 947.2 1.25 BAM 1.25 129.3 2.1 NAD 1.25 161.69 0.8 CAD 2.00
如果我要搜索['NAD']:-
期望输出:-在
使用numpy.argwhere可以有效地完成此操作:
numpy.argwhere
import pandas as pd, numpy as np df = pd.DataFrame([[947.2, 1.25, 'BAM 1.25'], [129.3, 2.1, 'NAD 1.25'], [161.69, 0.8, 'CAD 2.00']], columns=['Price', 'Rate p/lot', 'Total Comm']) res = np.argwhere(df.values.astype('<U3') == 'NAD') # array([[1, 2]], dtype=int64)
这将为您提供一个匹配条件的坐标数组。在
要获得一个元组:
对于字符串列表:
res = list(map(tuple, np.argwhere(np.logical_or.reduce(\ [df.values.astype('<U3') == i for i in np.array(['BAM', 'NAD'])]))))
如果有人想获取包含子字符串的单元格的位置,请参考。在
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 ('$') in str(x) else None )).values.nonzero() t = list(zip(row,column))
将^{}与startswith一起使用:
startswith
i, j = (df.applymap(lambda x: str(x).startswith('NAD'))).values.nonzero() t = list(zip(i, j)) print (t) [(1, 2)]
对于输入值列表,请使用:
使用
numpy.argwhere
可以有效地完成此操作:这将为您提供一个匹配条件的坐标数组。在
要获得一个元组:
^{pr2}$对于字符串列表:
如果有人想获取包含子字符串的单元格的位置,请参考。在
将^{} 与
startswith
一起使用:对于输入值列表,请使用:
^{pr2}$相关问题 更多 >
编程相关推荐