0

I am learning to use pandas and python. I want to look up a value from an excel in column A and print the value in column B. But Pandas can't find my value. Sometimes Pandas can find my value, but sometimes Pandas can't find my value, even though it is the correct value and is in the excel.

I make a syntax like this:

import pandas as pd
TARGET = "88807"

data = pd.read_excel('myexcel.xlsx')
row = data.loc[data['KOLOM_A'] == TARGET]
    if row.empty:
        print("DATA NOT FOUND IN THE DATABASE.")
    else:
        KOLOM_B = row.iloc[0]['KOLOM_B']
        print(KOLOM_B)
KOLOM A KOLOM B
54981 AGUS
88807 BUDI
86454 CAHYO
86280 DORI

Sometimes the data shows results but sometimes doesnt.

I think the problem on KOLOM A, data is TEXT or Number. But i already convert to Text and also to Number and still have same promblem. Please Help Me.

  • Can you be a bit more specific about the error you're getting, or how the output is different from what you expect? Also, if you can provide a sample dataframe that illustrates the problem (so others can replicate it), that would make it easier for people to answer. – ASGM Mar 15 '23 at 12:43
  • I found that a lot of time times this happens due to different dtype. '88807'(string) is not equal to 88807 (integer). They both appear the same on the screen. Check your dtypes and/or cast both to string or int. – Scott Boston Mar 15 '23 at 16:45

0 Answers0