Pandas how to find cell launch position using string

I have the following data frame. I want to find an index for a cell that starts with a specific row.

Example:

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

If I have a search ['NAD']: -

Expected Result: -

(1,2)
+4
source share
3 answers

Use applymapwith startswith:

i, j = (df.applymap(lambda x: str(x).startswith('NAD'))).values.nonzero()
t = list(zip(i, j))
print (t)
[(1, 2)]

For a list of input values, use:

L = ['NAD','BAM']
i, j = (df.applymap(lambda x: str(x).startswith(tuple(L)))).values.nonzero()
t = list(zip(i, j))
print (t)

[(0, 2), (1, 2)]
+1
source

You can do this effectively with 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)

This gives you an array of coordinates where your match will match.

To get one tuple:

res = next(map(tuple, np.argwhere(df.values.astype('<U3') == 'NAD')))

# (1, 2)

List of lines:

res = list(map(tuple, np.argwhere(np.logical_or.reduce(\
      [df.values.astype('<U3') == i for i in np.array(['BAM', 'NAD'])]))))
+1
source

For reference, if someone wants to get a position for a cell, it contains a substring.

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))
0
source

Source: https://habr.com/ru/post/1696147/


All Articles