Search pandas by index 'INT'

Viewed 33

I'm trying to create a search but I'm facing an error, according to some tests I can search for 'name', but I would like to search for 'number_order', does anyone have a solution? Remembering that 'number_order' cannot be changed inside the dataframe EX: 'number_order' : [202204000001] -> 'number_order' : ['202204000001']

import pandas as pd
import matplotlib.pyplot as plt

d = {'number_order' : [202204000001, 202204000002, 202204000003, 202204000004,
                       202204000005, 202204000006],
     'client' : ['Roger Nascimento', 'Rodrigo Peixato', 'Pedro',
'Rafael', 'Maria', 'Emerson'],
'value' : ['120', '187.74', '188.7', '300', '563.2', '198.0']
     }
     
df = pd.DataFrame(data = d)
     
src_field_data = '202004'

filtered_data = df['number_order']
filtered_data = df.loc[filtered_data.str.contains(f'^{src_field_data}', case = False)]
print(f'number_order FILTERED {filtered_data}\n')

I want to search like this example below, using only a part of the text:

import pandas as pd
import matplotlib.pyplot as plt

d = {'number_order' : [202204000001, 202204000002, 202204000003, 202204000004,
                       202204000005, 202204000006],
     'client' : ['Roger Nascimento', 'Rodrigo Peixato', 'Pedro',
'Rafael', 'Maria', 'Emerson'],
'value' : ['120', '187.74', '188.7', '300', '563.2', '198.0']
     }
     
df = pd.DataFrame(data = d)
     
src_field_data = 'R'

filtered_data = df['client']
filtered_data = df.loc[filtered_data.str.contains(f'^{src_field_data}', case = False)]
print(f'number_order FILTERED {filtered_data}\n')
1 Answers

Convert values to strings:

filtered_data = df.loc[filtered_data.astype(str).str.contains(f'^{src_field_data}', case = False)]
Related