Home > Back-end >  İn pandas. I need to search the information ı get from the user in the available data and print the
İn pandas. I need to search the information ı get from the user in the available data and print the

Time:08-10

data summery.xlsx
part number                   serial number
abcd Part number:1234 abcd    xyz

import pandas as pd
#my data
excel_file = 'summery.xlsx'
data = pd.read_excel(excel_file)

part number = input("enter part number")

I want to be able to get the serial number from the line by searching the part number information in the data

CodePudding user response:

Firstly, you can't have space in the variable name so rename it:

part_number = input("enter part number")

Then find out the row and print the ['serial number'] column:

print(data[data['part number'] == part_number]['serial number'])

CodePudding user response:

Basically, you need to filter your dataframe. The most intuitive way of indexing/filtering a dataframe in pandas is using loc

The overall syntax is df.loc[row_indexer, col_indexer] Once you have the required part number, just filter your dataframe as follows:
df.loc["part number" == part_number, "serial number"]

where part_number is the input from the user

CodePudding user response:

You can select columns from your data variable by doing the following:

part_number = data["number"]

You take the data that you read from the excel and access the information you want by putting the column name inside the brackets as a string. Additionally, if you need specific rows you can do:

part_number = data["number"][1]

And don't use spaces in variable names. It's gonna get treated as 2 different variables.

CodePudding user response:

My idea is, Firstly we can read it as a dataframe and search for the particular word in the sentence in the column.

wrd = df[df['col1'].fillna(0).str.contains("abcd",na=False)]

then we can extract the corresponding values of it.

  • Related