Home > Enterprise >  finding a row in pandas through input
finding a row in pandas through input

Time:05-02

I made a small script that iterates over a certain column through a given name and prints all its rows

I would like to make it search through its rows through a user input but not have to give it its full name.. last 3 letters would be sufficient for it.

enter image description here

If i give it the full name, for example - H516G067U it will find that memory

If something like 67U is given it will not find it, and this is exactly what im trying to do here.

What i've tried so far

import pandas as pd


file = "path"
df = pd.read_excel(f"{file}", "DDR5 UDIMM")
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)
sn = [x for x in df["IDC S/N"]]
memory = input("enter a number : ")
if memory in sn:
    print("true")
else:
    print("false")

CodePudding user response:

If you want the user input to be matched against the strings at any position, use the str.contains method. Also, if you want the query to be case-insensitive, make sure, both input and data are in the same case (e.g. using the lower() method on both). If I understand correctly, you don't want to just to print true or false, but return the actual rows matching the user input.

My suggeston is changing your code as follows:

import pandas as pd


file = "path"
df = pd.read_excel(f"{file}", "DDR5 UDIMM")


memory = input("enter a number : ")
df_out = df.loc[df.loc[:, 'IDC S/N'].str.lower().str.contains(memory.lower()), :]

So then df_out would contain all the rows where the serial number matches the user input.

CodePudding user response:

Use str accessor:

import re

file = "path"
df = pd.read_excel(f"{file}", "DDR5 UDIMM")
... # set_option

# memory <- 67U
memory = re.escape(input("enter a number : "))
if df['IDC S/N'].str[-3:] == memory
    print('true')
else:
    print('false')

CodePudding user response:

Pandas apply is pretty useful here.

Return a TRUE/FALSE column representing if the last 3 characters is the value:

memory = input("enter a number : ")
df["IDC S/N"].apply(lambda x: x[-3:] == memory)

Return a TRUE/FALSE column representing if value is in string at all:

memory = input("enter a number : ")
df["IDC S/N"].apply(lambda x: memory in x)

If there is NaN values in your column:

df["IDC S/N"].apply(lambda x: memory in x if type(x) != float else x)
  • Related