Filter data by column using user input value in python

I wanted to filter the data from a CSV file based on a value from the column SN. The column value is given by the user itself. I am using the below code but the same returns no values. Can someone please correct this? . The issue is in the line is_data = (data[‘SN’] == SN) The code works fine when I replace the SN with a value from the table

Below are the data and code data image

Expected output : Ex if the value is 1024314 the output should be expected output

   import pandas as pd
SN = input("insert SN number")

print(SN)

data = pd.read_csv("sample.csv")
is_data = (data['SN'] == SN)
print(is_data.head())
data_filter = data[is_data]
print(data_filter.shape)
print(data_filter.head())

Answer

input() returns a string and you are comparing it to integers. You should cast SN to integer:

data = pd.read_csv("sample.csv") is_data = (data['SN'] == int(SN))

Leave a Reply

Your email address will not be published. Required fields are marked *